"Global name is not defined"错误



我刚开始学习OOP,并且正在努力。这个问题可能正盯着我的脸。

def Travel():
    choice = str()   
    choice = input("Where will you search?nChoose F for Front or T for Trunkn")
    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()您返回了i但未定义它。

如果输入为"T",则将调用LocT(),并且由于Travel()范围内没有i,解释器将在外部查找i。而且外面也没有i,所以错误是可以理解的。

有几种方法可以修复您的代码,但由于我不知道您要做什么,所以我无法进一步提供帮助。

你的问题是关于静态和动态范围,而不是OOP。

相关内容

最新更新