Python NameError问题

时间:2017-12-10 17:54:53

标签: python-3.x list insert nameerror

我的代码有问题,说我有一个NameError,我的名字没有定义。虽然据我所知,我的名字已定义。

# Create UAS database list that was displayed in the file
uas_Stock = [["CS116",1],["CS117",1],["CS118",1],["CS119",1],["CS120",1]]

# Ask user to select which UAV they want to check out.
uas_out = str(input("Which UAV would you like to checkout? "))

# Append stock list to show UAS is checked out
if uas_out == CS116:
    list.insert(0,1, "0")
elif uas_out == CS117:
    list.insert(1,1, "0")
elif uas_out == CS118:
    list.insert(2,1, "0")
elif uas_out == CS119:
    list.insert(3,1, "0")
else:
    list.insert(4,1, "0")

我希望结果获取uas_out值并运行if / else语句并执行它所说的任何一个。然后,这将附加列表以给出特定列表0的值而不是1。

我输入uas_out的值时得到的错误是:

NameError: name '(whatever uas_out is/CS116/117/118/119/120)' is not defined.

1 个答案:

答案 0 :(得分:1)

CS116等是字符串,而不是变量,它们需要用引号括起来:

if uas_out == 'CS116':
    list.insert(0,1, "0")
elif uas_out == 'CS117':
    list.insert(1,1, "0")
elif uas_out == 'CS118':
    list.insert(2,1, "0")
elif uas_out == 'CS119':
    list.insert(3,1, "0")
else:
    list.insert(4,1, "0")