Python外部范围

时间:2017-12-05 10:49:36

标签: python

我收到一条错误,上面写着“阴影名称'从外部范围运行'”,我该如何解决?:

print("Logic Gate Calculator")


def AND(a, b):  # AND Gate
    a = int(a)
    b = int(b)
    if a == 1 and b == 1:  # AND Gate logic
        return 1
    else:
        return 0


def NAND(a, b):  # NAND Gate
    a = int(a)
    b = int(b)
    if a == 1 and b == 1:  # NAND Gate logic
        return 0
    elif a == 1 and b == 0:
        return 0
    elif a == 0 and b == 1:
        return 0
    else:
        return 1


def OR(a, b):  # OR Gate
    a = int(a)
    b = int(b)
    if a == 1:  # OR Gate Logic
        return 1
    elif b == 1:
        return 1
    else:
        return 0


def NOR(a, b):  # NOR Gate
    a = int(a)
    b = int(b)
    if a == 1 and b == 0:  # NOR Gate Logic
        return 1
    elif a == 0 and b == 1:
        return 1
    elif a == 0 and b == 0:
        return 1
    else:
        return 0


run = True
while run:
    def main():  # The main program
        run = True  # This extra run = True allows use to quit the program
        question = input("What type of gate do you want to use OR, AND, NOR, or NAND or (Q)uit")  # Logic Gate chooser
        if question == "AND" or question == "and" or question == "And":  # If the user selects AND
            a = input("Enter value for input 1 (1 or 0):")  # Getting the Logic Gate's 1st input
            b = input("Enter value for input 2 (1 or 0):")  # Getting the Logic Gate's 2nd input
            x = AND(a, b)  # Calling the AND Function
            print(x)
        elif question == "OR" or question == "or" or question == "Or":  # If the user selects OR
            a = input("Enter value for input 1 (1 or 0):")  # Getting the Logic Gate's 1st input
            b = input("Enter value for input 2 (1 or 0):")  # Getting the Logic Gate's 2nd input
            x = OR(a, b)  # Calling the OR Function
            print(x)
        elif question == "NOR" or question == "nor" or question == "Nor":  # If the user selects NOR
            a = input("Enter value for input 1 (1 or 0):")  # Getting the Logic Gate's 1st input
            b = input("Enter value for input 2 (1 or 0):")  # Getting the Logic Gate's 2nd input
            x = NOR(a, b)  # Calling the NOR function
            print(x)
        elif question == "NAND" or question == "nand" or question == "Nand":  # If the user selects NAND
            a = input("Enter value for input 1 (1 or 0):")  # Getting the Logic Gate's 1st input
            b = input("Enter value for input 2 (1 or 0):")  # Getting the Logic Gate's 2nd input
            x = NAND(a, b)  # Calling the NAND function
            print(x)
        elif question == "Q" or question == "q":  # Quiting the program
            run = False
        else:
            print("Please enter one of the shown logic gates")  # Error handling


    main()

0 个答案:

没有答案