我有一个循环问题

时间:2019-03-24 06:06:55

标签: python

只要最后一个问题的答案为“是”,我就希望游戏继续播放。我怎么做?该代码如下所示,它描述了一个非常简化的剪刀石头布游戏。

print("Welcome to rock paper scissors game")
p1 = input("Player 1 what do you choose? r/p/s")
p2 = input("Player 2 what do you choose? r/p/s")
if p1 == "r":
    if p2 == "r":
        print("no one wins. rematch")
    elif p2 == "p":
        print("player 2 wins! Congratulations")
    elif p2 == "s":
        print("player 1 wins! Congratulations")
    else:
        print("error")

if p1 == "p":
    if p2 == "r":
        print("player 1 wins! Congratulations")
    elif p2 == "p":
        print("no one wins. rematch")
    elif p2 == "s":
        print("player 2 wins! Congratulations")
    else:
        print("error")

if p1 == "s":
    if p2 == "r":
        print("player 2 wins! Congratulations")
    elif p2 == "p":
        print("player 1 wins! Congratulations")
    elif p2 == "s":
        print("no one wins. rematch")
    else:
        print("error")

response = input("Do you want to start a new game? yes/no")

2 个答案:

答案 0 :(得分:1)

exit = ''

while exit != 'yes':
    Your code here!
    .... 
    ....
    exit = input('do you want to exit? (yes/no)')

了解有关Python的循环

答案 1 :(得分:0)

您正在寻找一种称为“ do while”语句的东西。

根据我在python中的记忆,没有一个“ do”关键字可以像在其他语言中看到的那样。

这里是替代方法:

while True:
    # the code you already have

    # set response = input
    if response != "yes":
        break