[英]“Global name is not defined” error
我刚刚开始学习OOP,并为此而苦苦挣扎。 这个问题可能正盯着我。
def Travel():
choice = str()
choice = input("Where will you search?\nChoose F for Front or T for Trunk\n")
if choice == "F":
LocF()
elif choice == "T":
LocT(i)
def LocF():
print("Looking through the front of the car, you find a screwdriver.\nYou figure that might help a bit.")
inv = ("screwdriver")
return i
def LocT(i):
if i[0] == "screwdriver":
print("You use your screwdriver to pop the inside of the trunk door lock off.")
time.sleep(0.5)
print("You make it to class with seconds to spare.")
else:
print("You can't get to the trunk yet.")
Travel()
我假设您首先调用Travel()
函数。
如果调用Travel()
,则现在将输入一个值为'F'或'T'的字符串。
如果输入为“ F”, LocF()
调用LocF()
,在LocF()
返回i
但未定义它。
如果输入为“ T”, LocT()
调用LocT()
,并且由于Travel()
范围中没有i
,因此解释器将在外部查找i
。 而且外面也没有i
,所以错误是可以理解的。
有两种方法可以修复您的代码,但是由于我不知道您要做什么,因此我无济于事。
您的问题是关于静态和动态作用域,而不是OOP。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.