我希望用户在游戏结束后返回while循环

时间:2018-11-23 19:33:44

标签: python python-3.x

接受用户1的输入

player_1 = input("Enter your choice player_1 from rock,paper or scissor: ")

接受玩家2的输入

player_2 = input("Enter your choice player_2 from rock,paper or scissor: ")

剪刀石头布的规则1-石头覆盖纸2-敲打剪刀3-剪刀切纸

answer = []
while True:
    if player_1 == player_2:
        print("The game is tied sorry!!")
    elif player_1 == "paper" and player_2 == "scissor":
        print("scissor beats paper player_2 won")
    elif player_1 == "scissor" and player_2 == "paper":
        print("scissor beats paper player_1 won")
    elif player_1 == "rock" and player_2 == "paper":
        print("paper covers rock player_2 won")
    elif player_1 == "paper" and player_2 == "rock":
        print("paper covers rock player_1 won")
    elif player_1 == "scissor" and player_2 == "rock":
        print("rock breaks scissor player_2 won")
    elif player_1 == "rock" and player_2 == "scissor":
        print("rock breaks scissor player_1 won")

    answer = input("Do you want to play the game again? y or n : ")
    if answer == "y":
        continue 
    else:
        break

1 个答案:

答案 0 :(得分:0)

answer = []
black5 = True
while black5:

    player_1 = input("Enter your choice player_1 from rock,paper or scissor: ")
    player_2 = input("Enter your choice player_2 from rock,paper or scissor: ")

    if player_1 == player_2:
        print("The game is tied sorry!!")
    elif player_1 == "paper" and player_2 == "scissor":
        print("scissor beats paper player_2 won")
    elif player_1 == "scissor" and player_2 == "paper":
        print("scissor beats paper player_1 won")
    elif player_1 == "rock" and player_2 == "paper":
        print("paper covers rock player_2 won")
    elif player_1 == "paper" and player_2 == "rock":
        print("paper covers rock player_1 won")
    elif player_1 == "scissor" and player_2 == "rock":
        print("rock breaks scissor player_2 won")
    elif player_1 == "rock" and player_2 == "scissor":
        print("rock breaks scissor player_1 won")

    answer = input("Do you want to play the game again? y or n : ")
    if answer != "y":black = False

如果选择y,将继续,如果选择n,则退出。