“未定义全局名称”错误

时间:2017-02-04 04:53:57

标签: python python-3.x

我刚开始学习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()

1 个答案:

答案 0 :(得分:0)

我假设您先调用Travel()函数。

如果调用了Travel(),您现在将输入一个值为'F'或'T'的字符串。

如果输入为“F”,则LocF()会在LocF()中调用i,但您没有定义它。

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

有几种方法可以修复您的代码,但由于我不知道您尝试做什么,所以我无法继续帮助。

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

相关问题