2016-11-21 57 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: