[英]If elif don't work. What's wrong is in my code?
我試圖通過 PIN 碼進行安全設置,但它不起作用……怎么辦?
我一直在嘗試創建程序,該程序要求輸入 PIN。 如果你寫錯 3 次 PIN 碼,程序會和你說再見,然后程序結束。 此外,如果您編寫正確的 PIN 代碼,該程序允許您繼續下一步的代碼。
import time
money=1000
pokus=0
pin=7863
print("Vitajte v programe bankomat!")
for i in range(3):
vstup=int(input("Zadaj Váš 4-číselný PIN: "))
if vstup == 7863:
break
elif vstup != 7863:
pokus=pokus+1
print("Nesprávny PIN kód. Počet zostávajúcich pokusov: {}".format(3-pokus))
continue
if pokus == 3:
print("3x zadaný zlý PIN kód. DOVIDENIA!")
time.sleep(3)
quit()
elif pokus < 3:
continue
如果我啟動程序,它會告訴我“寫下你的 PIN 碼”。 如果我第一次輸入正確的 PIN 碼,程序將繼續下一步,但如果我輸入 3 次錯誤的 PIN 碼,程序也將繼續。 我一直試圖以其他方式修復它......我試圖通過while循環修復它,除了for循環,但它不起作用。 我不知道現在出了什么問題......我會感謝每一個反應和建議。
這將做同樣的事情
cnf = 0
print("Vitajte v programe bankomat!")
for i in range(3):
vstup=int(input("Zadaj Váš 4-číselný PIN: "))
if vstup == 7863:
cnf = 1
break
elif vstup != 7863:
print("Nesprávny PIN kód. Počet zostávajúcich pokusov: {}".format(3-i))
continue
if cnf !=1:
print("3x zadaný zlý PIN kód. DOVIDENIA!")
您的continue
位置錯誤。 刪除continue
:
import time
money=1000
pokus=0
pin=7863
print("Vitajte v programe bankomat!")
for i in range(3):
vstup=int(input("Zadaj Váš 4-číselný PIN: "))
if vstup == 7863:
break
elif vstup != 7863:
pokus=pokus+1
print("Nesprávny PIN kód. Počet zostávajúcich pokusov: {}".format(3-pokus))
# removed the continue here
if pokus == 3:
print("3x zadaný zlý PIN kód. DOVIDENIA!")
time.sleep(3)
quit()
elif pokus < 3:
continue
除非您絕對需要,否則我還建議使用break
而不是quit()
。
刪除第一個continue
和最后一個elif
(因為它沒用):
import time
money=1000
pokus=0
pin=7863
print("Vitajte v programe bankomat!")
for i in range(3):
vstup=int(input("Zadaj Váš 4-číselný PIN: "))
if vstup == 7863:
break
elif vstup != 7863:
pokus=pokus+1
print("Nesprávny PIN kód. Počet zostávajúcich pokusov: {}".format(3-pokus))
if pokus == 3:
print("3x zadaný zlý PIN kód. DOVIDENIA!")
time.sleep(3)
quit()
此代碼將執行相同的操作,以更清潔的方式。
import time
correct_pin_code = 7863
incorrect_inputs = 0
while(incorrect_inputs <= 3):
if(incorrect_inputs == 3):
print("Too many failed attemps, quitting")
quit()
input_pin = int(input("Enter your 4 digits PIN code"))
if(input_pin == correct_pin_code):
print("Correct PIN code")
break
else:
print("Wrong PIN code, " + str(3 - incorrect_inputs) + "attemps left")
time.sleep(3)
我認為您必須將條件中的嘗試次數與輸入一起包括在內。 你可以嘗試這樣的事情:
pin = 7863
test = 2
while int(input()) != pin and test > 0:
print("Incorrect pin. Try again.")
test -= 1
if test == 0:
print("quit")
quit()
print("OK. Continue the next phase.")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.