2016-11-21 4 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

を使用することができます:' 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() 5.

注、これはと同等に扱われるようself.increment_login_attempts(0)が、それをゼロに設定していないだろうに、それを設定するには、現在の試行番号、またはself.increment_login_attempts(5)に1を追加しますNone in if attempts:

あなたがゼロを扱うことができるようにしたい場合は試み> = self.increment_login_attemptsをすれば、あなたは `if attempts is not None:

関連する問題