[英]How do I make my python program generate the prime numbers in a certain interval just once instead of them being printed multiple times?
[英]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.