-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

when the button is pressed the LED does not go off is it a problem with the while loop? moredetailmoredetailsmoredetails

fatcat
  • 1
  • 4

1 Answers1

0

I think the problem is in the lines:

while restart == True:
    restart = False

Restart is set to False and then your code exits the loop immediately after the first iteration.

Try something like:

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

Let me know if that works.

Better still you could write:

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

Is this what you wish to happen?

jda5
  • 1,390
  • 5
  • 17