Python 代码无法与raspberry pi一起工作时遇到问题

Python 代码无法与raspberry pi一起工作时遇到问题,python,raspberry-pi,mu,Python,Raspberry Pi,Mu,按下按钮时,LED不会熄灭 while循环有问题吗? 更多详细信息更多详细信息我认为问题在于: 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:

按下按钮时,LED不会熄灭 while循环有问题吗?
更多详细信息更多详细信息我认为问题在于:

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
Restart设置为False,然后代码在第一次迭代后立即退出循环

尝试以下方法:

while restart == True:
    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循环?它只是结束while循环-它“中断”了循环
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