从子程序返回时,变量未赋值? Python 3?

时间:2016-08-21 05:59:18

标签: python

我正在尝试从子程序中返回一个值,但是当尝试在主程序中使用该变量时,会出现一个错误,说明它没有被赋值。

correct = 0
def subprogram():
    correct2 = 0
    loop = 0
    while loop == 0:
        loop2 = 0
        memberID = input("Please enter your member ID: ")
        if memberID == "1495":
            print("Login successful!")
            loop = 1
            correct2 = 1
        else:
            print("Login unsuccessful.")
            while loop2 == 0:
                decision = input("<T>ry Again or <E>xit to Menu? ")
                if decision == "t" or decision == "T":
                    print("Ok, restarting.")
                    print("")
                    loop2 = 1
                elif decision == "e" or decision == "E":
                    print("Ok, exiting to main menu.")
                    print("")
                    loop2 = 1
                    loop = 1
                    correct2 = 0
                else:
                    print("-----------------------------------------------------")
                    print("Sorry, this is not a valid answer. Please try again.")
                    print("-----------------------------------------------------")
        continue
    return correct2

#main
while correct == 0:
    print ("Are you a member?")
    member = input("<Y>es or <N>o? ")
    if member == "y":
        correct = subprogram()
        if correct2 == 0:
            correct = 0
        elif correct2 == 1:
            correct = 1
    elif member == "Y":
        correct = subprogram()
        if correct2 == 0:
            correct = 0
        elif correct2 == 1:
            correct = 1
    elif member == "n" or member == "N":
        print("Ok, not a problem! Welcome!")
        correct = 1
    else:
        print("-----------------------------------------------------")
        print("Sorry, this is not a valid answer. Please try again.")
        print("-----------------------------------------------------")
        correctVIP = 0
print("END")

我该如何解决此错误?三江源。

1 个答案:

答案 0 :(得分:0)

在主while循环中,您没有定义correct2变量,因此当您尝试执行此操作时会抛出错误:

if correct2 == 0:

您将子程序的结果分配给correct

correct = subprogram()

你可能想做的事情:

correct2 = subprogram()