我想要的是,如果您通過輸入 1 或 2 來選擇特定的事情,但即使您輸入 2,它也會始終運行“gen”選項(數字 1)。
while True:
a=int(input("pls choose what do you want to do.\n1=generates password \n2=acess saved passwords \n3=save passwords\nenter here:"))
if a == 1:
gen=True
break
if a==2:
see=True
break
if a==3:
save=True
break
else:
print('pls enter a valid respond\n----------------------------------------')
continue
if gen: #*<--it will always run this*
break
break
if see:
f = open("data.txt", "a")#*this did not run if typed '2'*
content=f.read()
f.close()
print(content)
uj5u.com熱心網友回復:
從 if 陳述句中洗掉 break
while True:
a=int(input("pls choose what do you want to do.\n1=generates password \n2=acess saved passwords \n3=save passwords\nenter here:"))
if a == 1:
gen=True
break---> Your code break when you type 1
if a==2:
see=True
break ---> Your code break when you type 2
if a==3:
save=True
break
else:
print('pls enter a valid respond\n----------------------------------------')
continue
if gen: #*<--it will always run this*
break
break
if see:
f = open("data.txt", "a")#*this did not run if typed '2'*
content=f.read()
f.close()
print(content)`enter code here`
uj5u.com熱心網友回復:
目前還不完全清楚你在問什么,但至少有兩件事你應該改變以完成我想象的你正在嘗試做的事情。首先,您應該使用eliffor 條件a == 2and a == 3:
if a == 1:
gen = True
break
elif a == 2:
see = True
break
elif a == 3:
save = True
else:
print(...)
continue
...
現在,當輸入不是 3(包括 1 或 2 時)時,您似乎會要求一個有效的回應,但我想您只希望在輸入不是 1、2 或 3 時列印此陳述句二、f = open("data.txt"...)沒有運行的原因是這個代碼塊在while回圈里面。每當部分代碼導致程式退出 while 回圈(例如break陳述句)時,回圈其余部分中的任何內容都不會被執行,包括該if see:塊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/472099.html
