簡體   English   中英

如果密碼正確,我如何發現3次打印“已授予訪問權限”?

[英]How do I spot 'Access granted' being printed 3 times if password is correct?

正確輸入密碼后,如何停止打印“已授予訪問權限”? 輸入“ swordfish”時,將授予訪問權限的命令打印三遍

在此處輸入鏈接說明

print ('The Database is password protected') # Says the Data base is password protected
print ('please enter password') #say please enter password

password = ('swordfish')
swordfish = 3

password = input()
if password == 'swordfish':

    print ('Access granted.')

else:

 if password != ('swordfish'):


    print ('wrong password.')

    print ('two attempts remain')

 else:
    password = input()

if password == 'swordfish':
    print ('Access granted.')
else:
    password = input()
if password == 'swordfish':
     print ('Access granted.')
else:
    print ('wrong password.')
    print ('one attempt remain')

    password = input()
    if password == 'swordfish':

if password != ('swordfish'):

     print ('You have been blocked from the database')

如果要停止打印並清理代碼,請使用循環:

編輯:這是使用for-else代碼塊的絕佳時機。 在此示例中, else子句僅在完整的for-loop已用盡時運行,即在輸入正確密碼的情況下,它不會運行“您已被數據庫阻止”(因為則執行了break語句)

password = 'swordfish'

print('The Database is password protected')

for attempts in range(2, -1, -1):
    if input('Please enter password') == password:
        print('Access granted.')
        break
    else:
        print('Wrong password.')
        print('%s attempts remain' % attempts)
else:
    print('You have been blocked from the database')
print ('The Database is password protected')
print ('please enter password') 

password = "swordfish"

for i in range(3):
    attempt = input("Enter the password: ")
    if attempt == password:
        print("Access Granted")
        break
    else:
        print("Wrong password")
        print(2 - i, "attempt(s) reamin")

    if i == 2:
        print("You have been blocked from the database")

您可以嘗試一個for循環 :)

for e in range(3):
    a=input()
    if a=="swordfish":
        print("Access Granted")
        break
    else:
        print("Wrong Password. You have "+str(3-(e+1))+" attempts left")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM