python - 即使已定义变量未定义错误
问题描述
我一直在尝试学习 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()
解决方案
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
推荐阅读
- django - 将嵌套对象序列化为当前父对象的平面属性
- algorithm - 在第三个列表中存在的两个python列表中查找公共索引
- python - 骰子游戏将随机骰子总数添加到变量中
- java - 无法使用 Spring Data JPA 创建 JOIN
- html - 将除第一个孩子之外的所有文本右对齐
- django - 无法使用 upload_to 动态创建路径
- typescript - 如何根据通用类型“T”初始化 TypeORM 存储库?
- typescript - 尝试扩充界面时,历史没有定义功能
- mongodb - MongoDB - 索引前缀如何工作?
- scrapy - 需要将 Scrapy 回调方法数据返回给调用函数