所以這是我的代碼
login = input("Whats the keyword?\n")
if login == "3faze":
print("Ok I will give you access!")
access = True
else:
print("Wrong! Try opening the program again!")
t.sleep(5)
access = False
if access == True:
while True:
action = input("What do you want to do?")
if action == "get passwords" or action == "Get Passwords" or action == "Get passwords" or action == "get Passwords":
print("Ok")
while True:
which = input("Which password do you want to get?")
if which == "google" or which == "Google":
print(str(passwords[0] passwords[1] passwords[2] passwords[3] passwords[4]))
close = input("Do you want to close?")
if close == "yes" or close == "Yes" or close == "yea" or close == "Yea" or close == "yea":
break
t.sleep(1)
else:
print("Ok")
t.sleep(1)
它說break的地方我想打破兩個嵌套回圈。感謝幫助的人。我是為密碼管理器制作的,因為您可能會說那里還有更多代碼,但我不想顯示它,因為它有我的密碼,如果需要,我將制作一個沒有這些密碼的模板。
uj5u.com熱心網友回復:
您可以將 while True 更改為變數并將其布林值更改為突破
password_found = False
while not password_found:
...
while not password_found:
close = input()
if close == 'yes':
password_found = True
uj5u.com熱心網友回復:
根據您的代碼,我嘗試進行修改以將兩個回圈合并為一個。
可以根據自己的需要更換continue。if close...:break
login = input("Whats the keyword?\n")
if login == "3faze":
print("Ok I will give you access!")
access = True
else:
print("Wrong! Try opening the program again!")
t.sleep(5)
access = False
if access is True:
skip = False
while True:
if not skip:
action = input("What do you want to do?")
if action == "get passwords" or action == "Get Passwords" or action == "Get passwords" or action == "get Passwords":
print("Ok")
skip = True
else:
continue
which = input("Which password do you want to get?")
if which == "google" or which == "Google":
print(str(passwords[0] passwords[1] passwords[2] passwords[3] passwords[4]))
close = input("Do you want to close?")
if close == "yes" or close == "Yes" or close == "yea" or close == "Yea" or close == "yea":
t.sleep(1)
skip = False
continue
# break
else:
print("Ok")
t.sleep(1)
uj5u.com熱心網友回復:
您可以再添加一個布爾變數,并將中間while回圈替換為while var_name.
login = input("Whats the keyword?\n")
if login == "3faze":
print("Ok I will give you access!")
access = True
else:
print("Wrong! Try opening the program again!")
t.sleep(5)
access = False
if access == True:
leave = False
while not leave:
action = input("What do you want to do?")
if action == "get passwords" or action == "Get Passwords" or action == "Get passwords" or action == "get Passwords":
print("Ok")
while True:
which = input("Which password do you want to get?")
if which == "google" or which == "Google":
print(str(passwords[0] passwords[1] passwords[2] passwords[3] passwords[4]))
close = input("Do you want to close?")
if close == "yes" or close == "Yes" or close == "yea" or close == "Yea" or close == "yea":
leave = True
t.sleep(1)
else:
print("Ok")
t.sleep(1)
除此之外,您無需檢查每種string可能性(例如Yes或yes),只需檢查close.lower() == "yes".
uj5u.com熱心網友回復:
還有 2 個提示:您可以將輸入轉換為小寫,這樣您就不必將其與兩個版本進行比較,例如:
if action == "get passwords" 或 action == "Get Passwords" -> if action.lower() == 'get passwords'
- 有更好的選擇
passwords[0] passwords[1] passwords[2] passwords[3] passwords[4]
但不清楚什么是“密碼”(字串、字母串列、數字串列?)
uj5u.com熱心網友回復:
嘗試使用函式并用 return 打破回圈。例如:
def sample():
while True:
while True:
if condition:
return
然后使用代碼中的函式:
sample()
uj5u.com熱心網友回復:
我對您的問題的解釋,我做了一些清理,使代碼對我來說更具可讀性,但這是個人決定:
actionLst = ("get passwords", "Get Passwords", "Get passwords", "get Passwords")
yesLst = ("yes", "Yes", "yea", "Yea", "yea")
googleLst = ("google", "Google")
login = input("Whats the keyword?\n")
if login == "3faze":
print("Ok I will give you access!")
access = True
else:
print("Wrong! Try opening the program again!")
t.sleep(5)
access = False
FirstTime = True
while access: # at this level already invariant ,
# don't use it in the loop itself !
if FirstTime:
action = input("What do you want to do?")
if action in actionLst: print("Ok")
else: continue
FirstTime = False
else:
which = input("Which password do you want to get?")
if which in googleLst:
print(str(passwords[0] passwords[1] passwords[2] passwords[3] passwords[4]))
close = input("Do you want to close?")
if close in yesLst:
break
t.sleep(1)
else:
print("Ok")
t.sleep(1)
# if you don't close will loop again !?
# so is not bulletproof ...
uj5u.com熱心網友回復:
嘗試在外回圈的最后一行添加第二個 break 關鍵字。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/411403.html
標籤:
