How to solve UnboundLocalError: local variable 'L1' referenced before assignment?

Viewed 105

I want this program to terminate the program when the user types exit.

I keep getting UnboundLocalError: local variable 'L1' referenced before assignment.

while True:
    L1 == input('Enter length of span AB in metre:')
    if L1 == "exit".lower() or L1 == "EXIT".upper():
        exit()
    elif L1 == "restart".lower():
        restart()
    try:
        L1 = float(L1)
    except ValueError:
        print("invalid input, please enter valid number")
        continue
    else:
        break
1 Answers

You're doing

L1 == input('Enter length of span AB in metre:')

Note the ==. You're comparing L1 against something, rather than setting it equal to it. You need only one =

L1 = input('Enter length of span AB in metre:')
Related