简体   繁体   English

我有一个循环问题

[英]I have an issue with loops

I want the game to loop as long as the answer to the last question is 'yes'. 只要最后一个问题的答案是“是”,我就希望游戏能够循环播放。 How do i do that? 我怎么做? The code is shown below, and it describes a very simplified rock paper scissors game. 该代码如下所示,它描述了一个非常简化的剪刀石头布游戏。

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")
exit = ''

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

Read about loops for Python 阅读有关Python的循环的信息

You are looking for something called a "do while" statement. 您正在寻找一种称为“ do while”语句的东西。

From what I remember in python, there is not a "do" keyword like you could see in other languages. 根据我在python中的记忆,没有其他语言可以看到的“ do”关键字。

Here is an alternative: 这是一个替代方案:

while True:
    # the code you already have

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM