Python记分牌

时间:2018-03-05 18:25:53

标签: python raspberry-pi

我正在尝试创建一个简单的python记分牌。最后,我将添加按钮来增加和减少值。这是我目前的代码,我怎样才能打印出来#34; New Game"并在某人获胜后重启循环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')

另外,我想添加一个条件,如果你输入&#34; REDRESET&#34; RED的得分= 3

1 个答案:

答案 0 :(得分:2)

如果您只是希望它在循环运行后等待5秒,只需等待sleep 5秒钟。添加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')