-1
enter code here
led = LED(17)
led.off()

player_1 = Button(2)
player_2 = Button(3)

time = random.uniform(2, 5)
sleep(time)
led.on()
twoscore = 0
onescore = 0

restart = True

while restart == True:
    restart = False
    if player_1.is_pressed:
        onescore += 1
        print("player one score: " + str(onescore))
        led.off()
        restart = True
    if player_2.is_pressed:
        twoscore += 1
        print("player two score: " + str(twoscore))
        led.off()
        restart = True

当按下按钮时,LED 不熄灭是 while 循环的问题吗?更多细节更多细节更多细节

4

1 回答 1

0

我认为问题在于:

while restart == True:
    restart = False

Restart 设置为 False,然后您的代码在第一次迭代后立即退出循环。

尝试类似:

restart = True

while restart == True:
    if player_1.is_pressed:
        onescore += 1
        print("player one score: " + str(onescore))
        led.off()
        restart = False
    if player_2.is_pressed:
        twoscore += 1
        print("player two score: " + str(twoscore))
        led.off()
        restart = False

让我知道这是否有效。

更好的是你可以写:

while True:
    if player_1.is_pressed:
        onescore += 1
        print("player one score: " + str(onescore))
        led.off()
        break
    if player_2.is_pressed:
        twoscore += 1
        print("player two score: " + str(twoscore))
        led.off()
        break

这是你希望发生的事情吗?

于 2020-11-12T21:20:12.743 回答