簡體   English   中英

我有一個循環問題

[英]I have an issue with loops

只要最后一個問題的答案是“是”,我就希望游戲能夠循環播放。 我怎么做? 該代碼如下所示,它描述了一個非常簡化的剪刀石頭布游戲。

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)')

閱讀有關Python的循環的信息

您正在尋找一種稱為“ do while”語句的東西。

根據我在python中的記憶,沒有其他語言可以看到的“ do”關鍵字。

這是一個替代方案:

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