我剛開始使用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
「不感覺很好「?笏? – delnan 2010-11-05 22:04:54