0

Can someone help me with fixing this problem? When I run this program I get the Error "UnboundLocalError: local variable 'TimerOnOff' referenced before assignment"

TimerOnOff = 0
Timer = 7.5
class Timeout():
    def start():
        Timer = 7.5
        if TimerOnOff == 1:
            for T in range(0, 75):
                Timer - 0.1
                time.sleep(0.1)
                print(Timer)
            TimerOnOff = 0

TimerOnOff = 1
Timeout.start()
Ser Vinci
  • 1
  • 2

1 Answers1

0

If you want to use global variables like TimerOnOff or Timer in function, you need do add a statement to specify that you are using global variables in local scope. So you need to add

global TimerOnOff
global Timer

in your function body. Full implementation would be:

import time
TimerOnOff = 0
Timer = 7.5
class Timeout():
    def start():
        global TimerOnOff
        global Timer
        Timer = 7.5
        if TimerOnOff == 1:
            for T in range(0, 75):
                Timer - 0.1
                time.sleep(0.1)
                print(Timer)
            TimerOnOff = 0

TimerOnOff = 1
Timeout.start()
Mert Köklü
  • 2,183
  • 2
  • 16
  • 20