python - 如何将登录尝试次数增加到一定数量?

标签 python python-3.x

我正在开发一个程序,该程序打印一些技术人员的基本用户信息,然后通过调用“increment_login_attempts()”方法来增加用户登录尝试的数量。此方法允许用户登录 3 次或更少,但如果超过该值,则会显示 elif 语句“您尝试的次数过多,设备已锁定”。前面的方法“reset_login_attempts”将登录尝试次数重置为 0。我的问题是,当我使用某个随机数参数调用该方法时,它不会打印任何整数值。当我包含重置值时,它只打印 0。任何帮助都会很棒,谢谢!

    class User:
    def __init__(self, first_name, last_name, email, expertise):
        self.first_name = first_name
        self.last_name = last_name
        self.email = email
        self.expertise = expertise
        self.login_attempts = 0

    def describe_user(self):
        print("The current user's summary: " + self.first_name.title() +
              self.last_name.title() + ", " + self.email + "," + self.expertise)

    def greet_user(self):
        print("Hello " + self.first_name.title() + self.last_name.title())

    def increment_login_attempts(self, attempts):
        self.login_attempts = attempts

        if attempts >= self.increment_login_attempts:
            self.login_attempts = attempts

            if attempts <= 3:
                attempts += 1
                print self.login_attempts

            elif attempts > 3:
                print("You have tried too many times, device is locked.")

    def reset_login_attempts(self, attempts):
        self.login_attempts = 0
        print self.login_attempts

user1 = User("Aye", "Bee", "ab@gmail.com", "Computer Hardware")
user2 = User("Cee", "Dee", "cd@mozila.com", "Computer Networks")
user3 = User("Ee", "Eff", "ef@pillowz.com", "Operating Systems")

print("The Malware Analyst is " + user1.first_name.title() + ' ' + user1.last_name.title())
print("Her expertise is in " + user1.expertise)
print("Her email is " + user1.email) 
print("-------------------------------------")

print("The Security Engineer is " + user2.first_name.title() + ' ' + user2.last_name.title())
print("His expertise is in " + user2.expertise)
print("His email is " + user2.email)
print("-------------------------------------")

print("The Pen Tester is " + user3.first_name.title() + ' ' + user3.last_name.title()) 
print("His expertise is in " + user3.expertise)
print("His email is " + user3.email)

# What you are doing is incrementing the login attempts by using calling increment_login_attempts
user1.increment_login_attempts(33)
user1.reset_login_attempts(1)

最佳答案

if attempts >= self.increment_login_attempts: 这不会执行您认为的操作:

如果attempts大于self.increment_login_attempts函数的内存位置...

由于 attempts 为 33,并且 self.increment_login_attempts 的内存地址是一个巨大的数字,因此它会跳过函数的其余部分。

def increment_login_attempts(self, attempts=None):

    if attempts:
        self.login_attempts = attempts
    else:
        self.login_attempts += 1

    if self.login_attempts <= 3:
        print self.login_attempts
    else:
        print("You have tried too many times, device is locked.")

可以调用: self.increment_login_attempts() 将当前尝试次数加 1,或 self.increment_login_attempts(5) 将其设置为 5。

请注意,self.increment_login_attempts(0) 不会将其设置为零,因为这在 if attempts: 中被视为等同于 None .

如果您希望能够处理零,则可以使用if attempts is not None:

关于python - 如何将登录尝试次数增加到一定数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40728003/

相关文章:

python - 在同一项目中使用 Python2 和 Python3

python-3.x - python : ICD-10 RegEx

python-3.x - Elasticsearch聚合给我2个结果,而不是一个结果

python - django heroku foreman 启动错误代码 1

python - 使用 Pandas 将多个时间序列行合并为一行

python-3.x - 如何在 Python 中将字节和字节流连接成字节流?

python - BeautifulSoup 4.0b 标记按摩

python - 为什么在这个函数中使用self?

python - cv2 轮廓无法检测到某些形状

python - 如何根据对象实例自定义 Django 内联管理表单