我通过阅读txt文件制作了2个列表。 1列表包含项目的代码,另一个列表包含相应的价格。我正在尝试让用户输入代码,然后在代码列表中搜索该代码并使用以下代码打印相应的价格:
while code != "9999":
code = input("Enter 4-digit item code [or 9999 to stop]: ")
if code in priceNumList:
y = priceList.index(code)
print("Item found: ", priceList[y])
elif code not in priceNumList:
print("Item not found.")
每当我运行它并输入列表中的代码时,我会收到以下错误:
Traceback (most recent call last):
File "C:/Users/Jeste/Documents/Python Projects/Homework/wakemart.py", line
36, in <module>
y = priceList.index(code)
ValueError: '####' is not in list
我确信我输入的价格代码与我列表中的元素完全匹配,但程序告诉我它不在那里。我做错了什么?
答案 0 :(得分:0)
没关系我弄明白了!问题是“y = priceList.index(code)”应该是“y = priceNumList.index(code)”这就是我制作两个列表所得到的! :P