首页 > 解决方案 > 即使已定义变量未定义错误

问题描述

我一直在尝试学习 Python 并创建了一个小程序,它要求预算并推荐仪器我不知道如何解决这个问题,说“NameError: name 'instrument is not defined”。我几乎可以肯定它会发生,因为我已经将 instrument_choice 函数放在那里,没有它,它就可以工作。

这是我的代码:

list1 = ["Piano", "Keys"]
list2 = ["Piano", "Keys", "Bass", "Plucks", "Pads", "Guitar"]
list3 = ["Bass"]
list4 = ["Guitar", "Bass"]
list5 = ["Synth"]

print("Hi! Welcome to VST instrument selector.")

def instrument_choice():
    instrument = input("Which instrument do you want to get? (Keys, Bass, etc.) ").capitalize()

    while True:
        if not (instrument in list2) and (instrument != "!help"):
            print("This instrument type is not valid. Try again. For the list of instruments, type !help.")
            instrument_choice()
        if instrument in list2:
            break
        if instrument == "!help":
            print("""Available instruments types are Piano, Keys, Bass, Plucks, Pads & Guitar.
Also you need to only write numbers while writing your budget do not type '300 USD' or '300 dollars'. Just type '300'.""")
            instrument_choice()
            break

instrument_choice()

def nobudget():
    while True:
        try:
            budget = int(input("What's your budget? (in $) "))
        except ValueError:
            print("Please use digits.")
            continue
        else:
            break

    if (budget < 169):
        print("There is no plugin available for this budget. Try to raise your budget.")
        while True:
            nobudget()
            break
    if (instrument in list2) and (budget >= 499):
        print("You can get Spectrasonic Omnisphere 2, it's $499!")
    if (instrument in list4) and (budget >= 299):
        print("You can get Spectrasonic Trillian, it's $299!")
    if (instrument in list1) and (budget >= 169):
        print("You can get Addictive Keys 2, it's $169!")
    if (instrument in list1) and (budget >= 399):
        print("You can get Spectrasonics Keyscape, it's $399!")
    if (instrument in list1) and (budget >= 199):
        print("You can get Lounge Lizard EP-4, it's $199!")
    if (instrument in list5) and (budget >= 189):
        print("You can get Xfer Serum, it's $189!")
    if (instrument in list3) and (budget >= 299):
        print("You can get MODO Bass, it's $299! It also has a lite version, MODO Bass SE for $149!")

nobudget()

标签: python

解决方案


instrument是一个局部变量,这意味着它只能在instrument_choice函数中访问。如果要在nobudget函数中使用,需要全局声明。

这对我有用:

list1 = ["Piano", "Keys"]
list2 = ["Piano", "Keys", "Bass", "Plucks", "Pads", "Guitar"]
list3 = ["Bass"]
list4 = ["Guitar", "Bass"]
list5 = ["Synth"]

print("Hi! Welcome to VST instrument selector.")


def instrument_choice():
    instrument = input(
        "Which instrument do you want to get? (Keys, Bass, etc.) ").capitalize()

    while True:
        if not (instrument in list2) and (instrument != "!help"):
            print(
                "This instrument type is not valid. Try again. For the list of instruments, type !help.")
            instrument_choice()
        if instrument in list2:
            break
        if instrument == "!help":
            print("""Available instruments types are Piano, Keys, Bass, Plucks, Pads & Guitar.
Also you need to only write numbers while writing your budget do not type '300 USD' or '300 dollars'. Just type '300'.""")
            instrument_choice()
            break
    
    return instrument # HERE


instrument = instrument_choice() # HERE


def nobudget(instrument): # HERE
    while True:
        try:
            budget = int(input("What's your budget? (in $) "))
        except ValueError:
            print("Please use digits.")
            continue
        else:
            break

    if (budget < 169):
        print("There is no plugin available for this budget. Try to raise your budget.")
        while True:
            nobudget()
            break
    if (instrument in list2) and (budget >= 499):
        print("You can get Spectrasonic Omnisphere 2, it's $499!")
    if (instrument in list4) and (budget >= 299):
        print("You can get Spectrasonic Trillian, it's $299!")
    if (instrument in list1) and (budget >= 169):
        print("You can get Addictive Keys 2, it's $169!")
    if (instrument in list1) and (budget >= 399):
        print("You can get Spectrasonics Keyscape, it's $399!")
    if (instrument in list1) and (budget >= 199):
        print("You can get Lounge Lizard EP-4, it's $199!")
    if (instrument in list5) and (budget >= 189):
        print("You can get Xfer Serum, it's $189!")
    if (instrument in list3) and (budget >= 299):
        print("You can get MODO Bass, it's $299! It also has a lite version, MODO Bass SE for $149!")


nobudget(instrument) # HERE

推荐阅读