Python记分板

2024-05-19 12:34:45 发布

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

我正在尝试创建一个简单的python记分板。最后,我将添加按钮来增加和减少值。这是我当前的代码,我如何让它打印“新游戏”并在有人获胜后5秒重新启动循环?在

RedScore = 0
BlueScore = 0

while RedScore <= 5 and BlueScore <= 5:
    if RedScore == 5:
        print('RED WINS')
        break
    elif BlueScore == 5:
        print('BLUE WINS')
        break
    else:
        x = input("Who Scored? ")
        if x == 'Red':
            RedScore += 1
            print(RedScore)
        elif x == 'Blue':
            BlueScore += 1
            print(BlueScore)
        else:
            print('Bad Input')

另外,我想添加一个条件,如果你输入“REDRESET”,RED的分数将=3


Tags: and代码游戏ifred按钮elseprint
1条回答
网友
1楼 · 发布于 2024-05-19 12:34:45

如果您只想让它等待5秒,那么在循环运行后sleep5秒钟。添加REDRESET与拥有另一个elif一样简单

from time import sleep
while RedScore <= 5 and BlueScore <= 5:
    if RedScore == 5:
        print('RED WINS')
        sleep(5)
        RedScore = BlueScore = 0 
    elif BlueScore == 5:
        print('BLUE WINS')
        sleep(5)
        BlueScore = RedScore = 0
    else:
        x = input("Who Scored? ")
        if x == 'Red':
            RedScore += 1
            print(RedScore)
        elif x == 'Blue':
            BlueScore += 1
            print(BlueScore)
        elif x == 'REDRESET':
            RedScore = 3
        else:
            print('Bad Input')

相关问题 更多 >