Python 3.x 无法跳出循环

Python 3.x 无法跳出循环,python-3.x,while-loop,Python 3.x,While Loop,我正试图用类和对象编写一个智囊团游戏,目前我被一些循环所困扰 while True: # create a combination # test the combination while game_won == False: print(scoreboard) # player input combination # combination is tested then added to scoreboard tries_left = tries_left+1

我正试图用类和对象编写一个智囊团游戏,目前我被一些循环所困扰

while True:
# create a combination
# test the combination
while game_won == False:
    print(scoreboard)
    # player input combination
    # combination is tested then added to scoreboard
    tries_left = tries_left+1
    if game_won == True:
        print(You Won!)
        input = Play Again? Y/N
    if tries_left == 10:
        print(You Lost!)
        input = Play Again? Y/N
如何从上一条if语句返回while True->创建组合?(如果左=10:)

怎么了
  • 您的第一个
    虽然True
    中没有任何内容,但如果希望代码位于循环中,则需要在其下缩进代码

  • 有一些打字错误、缺少注释字符和引号

需要发生什么
  • 当嵌套的while循环
    while game_won==True
    退出时,代码将返回循环父循环
    while True
    ,如果用户愿意,它将重播游戏
您的代码已修复(有一些改进) 下面是如何让游戏永远循环(根据用户的意愿)

改进
  • 布尔逻辑使用
    not
    而不是
    myBool==False

  • 为True时:
    更改为
    ,而用户希望播放时:

  • 用户输入被删除,忽略空格和小写

  • 将赢得的比赛更改为完成的比赛

  • 改为使
    尝试向左倒计时

怎么了
  • 您的第一个
    虽然True
    中没有任何内容,但如果希望代码位于循环中,则需要在其下缩进代码

  • 有一些打字错误、缺少注释字符和引号

需要发生什么
  • 当嵌套的while循环
    while game_won==True
    退出时,代码将返回循环父循环
    while True
    ,如果用户愿意,它将重播游戏
您的代码已修复(有一些改进) 下面是如何让游戏永远循环(根据用户的意愿)

改进
  • 布尔逻辑使用
    not
    而不是
    myBool==False

  • 为True时:
    更改为
    ,而用户希望播放时:

  • 用户输入被删除,忽略空格和小写

  • 将赢得的比赛更改为完成的比赛

  • 改为使
    尝试向左倒计时


谢谢您的解释!谢谢你的解释!
# Assume user wants to play when the program runs
user_wants_to_play = True
ans = ""

# loop the entire game while the user wishes to play
while user_wants_to_play:
    # Create the combination
    # Test the combination

    # Game isn't done when started
    game_done = False
    tries_left = 10  # Arbitrary number I chose

    while not game_done:
        # Play the game
        print("Scoreboard")

        # Subtract one to tries left
        tries_left -= 1

        if game_done:
            print("You won!")

        elif tries_left == 0:
            print("You lost!")
            game_done = True

        # if users answer was 'n'
    ans = input("Play again? Y/N \n")
    if ans.strip().upper() == 'N':
        user_wants_to_play = False