2010-11-05 49 views
2

我剛開始使用Python,並決定嘗試從Python Wiki這個小項目:我可以改進當前的Python代碼嗎?

寫密碼猜測程序來跟蹤多少次用戶輸入的密碼錯誤。如果超過3次,則打印您被拒絕訪問。並終止程序。如果密碼正確,則打印您已成功登錄並終止程序。

這是我的代碼。它的作品,但它只是不覺得正確與這些環路中斷嵌套如果語句

# Password Guessing Program 
# Python 2.7 

count = 0 

while count < 3: 
    password = raw_input('Please enter a password: ') 
    if password != 'SecretPassword': 
     count = count + 1; 
     print 'You have entered invalid password %i times.' % (count) 
     if count == 3: 
      print 'Access Denied' 
      break 
    else: 
     print 'Access Granted' 
     break 
+0

「不感覺很好「?笏? – delnan 2010-11-05 22:04:54

回答

7

您可以用下面的函數替換while循環:

def login(): 
    for i in range(3): 
     password = raw_input('Please enter a password: ') 
     if password != 'SecretPassword': 
      print 'You have entered invalid password {0} times.'.format(i + 1) 
     else: 
      print 'Access Granted' 
      return True 
    print 'Access Denied' 
    return False 

您可能還需要考慮使用getpass模塊。

+2

真的很小挑剔:應該是'.format(i + 1)'。另外,'getpass'是實現真正代碼的途徑,但對於初學者的練習並不是真的需要。 – delnan 2010-11-05 22:08:13

+0

@delnan:+1謝謝,修正。 – 2010-11-05 22:10:31

+0

當我運行這段代碼時,我什麼也得不到。我如何「調用」這個功能? – 2010-11-05 22:32:03

0

您可以將if語句帶出while循環。

# Password Guessing Program 
# Python 2.7 

count = 0 
access = False 

while count < 3 and not access: 
    password = raw_input('Please enter a password: ') 
    if password != 'SecretPassword': 
     count += 1 
     print 'You have entered invalid password %i times.' % (count) 
    else: 
     access = True 

if access: 
    print 'Access Granted' 
else: 
    print 'Access Denied' 
+0

我不是超級有經驗的Python,所以可能有一些語法問題。 – thattolleyguy 2010-11-05 22:09:22

+0

是的,它確實包含sytnax錯誤。 &&在Python中無效,然後它抱怨沒有定義名稱'false'。 – 2010-11-05 22:13:23

+0

有一些修改應該修復語法(我認爲)。我認爲總體邏輯通過擺脫休息來回答問題。 – thattolleyguy 2010-11-05 22:27:54

2
granted = False # default condition should be the least dangerous 
for count in range(3): 
    password = raw_input('Please enter a password: ') 
    if password == 'SecretPassword': # no need to test for wrong answer 
     granted = True 
     break 
    print 'You have entered invalid password %i times.' % (count+1) # else 

if granted: 
    print 'Access Granted' 
else: 
    print 'Access Denied' 
+0

快速的問題,當你這樣做「爲範圍計數(3):」什麼是計數的初始值? – 2010-11-05 22:18:59

+0

@ 2012年沉默:這是零,謝謝! – 2010-11-05 22:21:48

3

我不反對環路/如果「勢在必行」的感覺,但我會從你的「演示」分離的「商業邏輯」:

count = 0 

# Business logic 
# The correct password and the maximum number of tries is placed here 
DENIED, VALID, INVALID = range(3) 
def verifyPassword(userPassword): 
    global count 
    count += 1 

    if count > 3: 
     return DENIED 
    elif password == 'SecretPassword': 
     return VALID 

    return INVALID 

# Presentation 
# Here you do the IO with the user 
check = INVALID 
while (check == INVALID): 
    password = raw_input('Please enter a password: ') 
    check = verifyPassword(password) 

    if check == INVALID: 
     print 'You have entered invalid password %i times.' % (count) 
    elif check == VALID: 
     print 'Access Granted' 
    else # check == DENIED 
     print 'Access Denied' 
+0

我想你想聲明'count'是全局的。 – aaronasterling 2010-11-06 02:11:43

+0

哦,是的,對不起。 – rsenna 2010-11-06 02:50:40

相關問題