循环退出而不是返回到循环顶部-Python

时间:2017-12-10 17:11:06

标签: python-3.x while-loop

我在while循环结束时遇到问题。完成游戏后,程序会询问您是否要在输入之后再次玩游戏,然后将播放器变量设置为false以便继续循环,但如果输入任何内容,程序将退出。

# Importing Random
from random import *

win = 0
lose = 0
draw = 0

print("[y/Y] Play game")
print("[n/N] Quit")

answer = input("Do you wanna play a game?: ")

if answer == "y" or answer == "Y":
    # Show score of game
    print("Score:", win,"wins,", draw, "draws,", lose,"losses")

    #create a list of play options
    plays = ["ROCK", "PAPER", "SCISSORS"]

    #assign a random play to the computer
    computer = choice(plays)

    #set player to False
    player = False

    while player == False:
        #set player to True
        player = input("Choose one: rock, paper or scissors: ").upper()
        if player == computer:
            print("Draw, try again")
            # Add 1 to the draw score
            draw += 1

        elif player == "ROCK":
            if computer == "Paper":
                print("You lose!", computer, "covers", player)
                # Add 1 to the lose score
                lose += 1

            else:
                print("You win!", player, "smashes", computer)
                # Add 1 to the win score
                win += 1

        elif player == "PAPER":
            if computer == "Scissors":
                print("You lose!", computer, "cut", player)
                # Add 1 to the lose score
                lose += 1

            else:
                print("You win!", player, "covers", computer)
                # Add 1 to the win score
                win += 1

        elif player == "SCISSORS":
            if computer == "Rock":
                print("You lose...", computer, "smashes", player)
                # Add 1 to the lose score
                lose += 1

            else:
                print("You win!", player, "cut", computer)
                # Add 1 to the win score
                win += 1

        else:
            print("That's not a valid play. Check your spelling!")

        # Ask the player for a new game
        new_game = input("Do you wanna play again?: ")

        if answer == ["Y", "y", "Yes", "yes", "YES"]:
            # Set player to false again to continue loop
            player = False
            computer = choice(plays)

        elif answer == ["N", "n", "no", "No", "NO"]:
            global flag
            flag = 1
            exit()
    if flag == 1:
        exit()

elif answer == "n" or answer == "N":
    exit()

问题在于这一部分  #向玩家询问新游戏             new_game =输入("你想再玩一次吗?:")

        if answer == ["Y", "y", "Yes", "yes", "YES"]:
            # Set player to false again to continue loop
            player = False
            computer = choice(plays)

        elif answer == ["N", "n", "no", "No", "NO"]:
            global flag
            flag = 1
            exit()
    if flag == 1:
        exit()

elif answer == "n" or answer == "N":
    exit()

1 个答案:

答案 0 :(得分:0)

解决了,我不得不用答案

更改变量new_game