Python 在游戏中重新启动while循环

Python 在游戏中重新启动while循环,python,python-3.x,while-loop,Python,Python 3.x,While Loop,我正在尝试用python编写一个石头剪刀游戏。代码如下: D_0 = {1: "rock", 2: "scissors", 3: "paper"} from random import randint play = False name = input("What is your name?: ") print("%s you have to pick among rock,paper and scissors." % name) while play == False: p_1 =

我正在尝试用python编写一个石头剪刀游戏。代码如下:

D_0 = {1: "rock", 2: "scissors", 3: "paper"}
from random import randint
play = False
name = input("What is your name?: ")
print("%s you have to pick among rock,paper and scissors." % name)

while play == False:
    p_1 = input("which one?")
    computer = D_0[randint(1, 3)]
    print("my choice is: ",computer)
    if p_1 == computer:
        print("it's a draw")
    elif p_1 == "scissors" and computer == "paper":
        print("you won!")
    elif p_1 == "paper" and computer == "scissors":
        print("you lost!")
    elif p_1 == "scissors" and computer == "rock":
        print("you lost!")
    elif p_1 == "rock" and computer == "paper":
        print("you lost!")
    elif p_1 == "rock" and computer == "scissors":
        print("you won!")
    elif p_1 == "paper" and computer == "rock":
        print("you won!")
    else:
        print("Invalid input")
    break
again = input("do you want another round?:")
if again == "yes":
    play = False
else:
    play = True
程序运行良好,但我希望它能询问玩家是否想再进行一轮。如果答案是“是”,程序必须重新启动循环。 问题是我不知道怎么做。我知道这可能与真与假有关,我试图做一些你在代码中看到的事情,但没有成功。
请帮帮我

一个简单的修复方法可能是将while循环设置为
True
,并继续循环直到中断执行:

D_0 = {1: "rock", 2: "scissors", 3: "paper"}
from random import randint

name = input("What is your name?: ")
print("%s you have to pick among rock,paper and scissors." % name)

while True:
    p_1 = input("which one?")
    computer = D_0[randint(1, 3)]

    print("my choice is: ", computer)

    if p_1 == computer:
        print("it's a draw")
    elif p_1 == "scissors" and computer == "paper":
        print("you won!")
    elif p_1 == "paper" and computer == "scissors":
        print("you lost!")
    elif p_1 == "scissors" and computer == "rock":
        print("you lost!")
    elif p_1 == "rock" and computer == "paper":
        print("you lost!")
    elif p_1 == "rock" and computer == "scissors":
        print("you won!")
    elif p_1 == "paper" and computer == "rock":
        print("you won!")
    else:
        print("Invalid input")

    again = input("do you want another round?:")
    if again != "yes":
        break

一个简单的修复方法可能是将while循环设置为
True
,并继续循环直到中断执行:

D_0 = {1: "rock", 2: "scissors", 3: "paper"}
from random import randint

name = input("What is your name?: ")
print("%s you have to pick among rock,paper and scissors." % name)

while True:
    p_1 = input("which one?")
    computer = D_0[randint(1, 3)]

    print("my choice is: ", computer)

    if p_1 == computer:
        print("it's a draw")
    elif p_1 == "scissors" and computer == "paper":
        print("you won!")
    elif p_1 == "paper" and computer == "scissors":
        print("you lost!")
    elif p_1 == "scissors" and computer == "rock":
        print("you lost!")
    elif p_1 == "rock" and computer == "paper":
        print("you lost!")
    elif p_1 == "rock" and computer == "scissors":
        print("you won!")
    elif p_1 == "paper" and computer == "rock":
        print("you won!")
    else:
        print("Invalid input")

    again = input("do you want another round?:")
    if again != "yes":
        break

尝试将4个空格移到最后5行代码中。尝试将4个空格移到最后5行代码中。最好使用列表或成对记录,而不是使用elif树。@Vikas Damodar你能解释更多吗?比如
列表赢[“剪刀纸”,…]
列表输=[“剪纸”,…]
,然后检查像
p\u 1+“+列表中的计算机可以避免使用elif树。最好使用列表或成对记录,而不是使用elif树。@Vikas Damodar,你能解释更多吗?比如
列表赢[“剪纸”,…]
列表输=[“剪纸”,…]
,然后勾选
p\u 1+“+列表中的计算机可以避免使用elif树。