我正在开发一个程序,该程序打印一些技术人员的基本用户信息,然后通过调用“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/