2016-11-21 88 views
1

我正在开发一个程序,打印有关某些技术工人的基本用户信息,然后通过调用“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", "[email protected]", "Computer Hardware") 
user2 = User("Cee", "Dee", "[email protected]", "Computer Networks") 
user3 = User("Ee", "Eff", "[email protected]", "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) 
+1

'如果尝试> = self.increment_login_attempts:'这不会做你认为它的作用:如果'attempts'比内存大'self.increment_login_attempts'函数的位置... – TemporalWolf

回答

1

if attempts >= self.increment_login_attempts:这不会做你认为它的作用:

如果attemptsself.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()一个添加到当前的尝试次数,或self.increment_login_attempts(5)将其设置为5

注意,self.increment_login_attempts(0)不会将其设置为零,因为这是等效处理, None in if attempts:

如果你想能够处理零,您可以使用if attempts is not None:

相关问题