如何解决未绑定本地错误:赋值前引用的局部变量"L1"?



我想让这个程序在用户键入exit时终止程序。

我一直得到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

你在做

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

注意==。你将L1与某物进行比较,而不是让它等于它。您只需要一个=

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

最新更新