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

2024-04-16 12:19:17 发布

您现在位置:Python中文网/ 问答频道 /正文

从用户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

Tags: orandanswerinputyourpaperplayerprint
1条回答
网友
1楼 · 发布于 2024-04-16 12:19:17
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将退出

相关问题 更多 >