我对Python很陌生。我正在使用最新版本。我正在使用的代码如下:简单查询:为什么说这个变量是未定义的?
def tts():
print('(0) Input a new list')
print('(1) Print the current list')
choice = int(input('Enter your choice: '))
if choice == 0:
newList = eval(input('Enter a new list of prices: '))
tts();
elif choice == 1:
return newList
所以,我开始通过运行函数tts()。它打印它应该的内容并提示我输入。我输入0,并输入一个列表,如[20,10,30]。在点击回车后,现在不应该将变量newList分配给我输入的列表吗?它似乎没有工作。如果我稍后在Shell中键入newList,则表示newList未定义。基于同样的原因,如果我稍后选择1作为我的选择,它会说newList没有被定义。
我将不胜感激一些建议。我感到非常沮丧,我怀疑这是一个我只是不知道的非常微不足道的问题。
fyi'eval'是危险的。 –