![](/img/trans.png)
[英]Break out of the 'nested loop' when condition is met, and then continue the loop of the parent loop
[英]Using a loop to continue or break the program if condition is met
我今天有生以來第一次開始擺弄 python,我決定制作某種彩票“游戲”,其中有一個預先設定的注冊用戶列表,即“玩家”,將通過使用“玩家”變量。 如何使用循環來使用播放器變量輸入並搜索它是否存在於列表播放器中? 可能這是一個非常愚蠢的問題,但我很好奇如何去做,或者如果有另一種更好/更快的方法,我會很感激有人啟發我。 謝謝:
lucky_numbers = [4, 8, 15, 17, 23, 42]
players = ["Kevin", "Stacey", "Jim", "Monica", "Donnie"]
player = input("Please enter your name: ")
if player in players:
print(f"Ok {player}, move on..")
else:
print(f"Uh oh, you are not on the players list!")
input_numbers = list(map(int, input("Enter 6 lucky numbers: ").split()))
if input_numbers == lucky_numbers:
print("Congratulations! YOU WON!")
else:
print("Sorry. You guessed wrong!")
錯誤在於使用行player = print(input("Please enter your name: "))
如果您打印它,則不會分配 player 變量。
應該是player = input("Please enter your name: ")
代替。
對不起,我看了好幾遍,還是不太明白你的意思。
您是否希望用戶重復輸入姓名,直到該姓名出現在列表中? 一旦名字在列表中就可以進入下一階段?
lucky_numbers = [4, 8, 15, 17, 23, 42]
players = ["Kevin", "Stacey", "Jim", "Monica", "Donnie"]
is_login_success = False
while True:
name = input("Please enter your name or enter `Exit` to exit game\n> ").capitalize().strip()
if name == "Exit":
is_login_success = False
print("Bye bye! Hope see you soon.")
break
elif name in players:
is_login_success = True
print(f"Ok {name}, move on..")
break
else:
print("Uh oh, you are not on the players list!")
print()
if is_login_success:
# play game
另外,提出一些關於游戲的建議:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.