2016-11-21 3 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: это не делает то, что вы думаете, он делает:

Если 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() добавить единицу к текущему номеру попытки, или self.increment_login_attempts(5) установить его на 5.

Примечание, self.increment_login_attempts(0) не установит его к нулю, так как это лечится равносильно None в if attempts:.

Если вы хотите, чтобы быть в состоянии справиться с нуля, вы можете использовать if attempts is not None: