python name error: NameError: name 'Jewels' is not defined
下面的代码有什么错误?在哪里定义列表变量jewels?
1 2 | for jew in range(0,Total_Jewels): Jewels[jew]=int(raw_input("Please Enter approx price for Jewel#"+str(jew+1))) |
错误
Please Enter approx price for Jewel#111
--------------------------------------------------------------------------- NameError Traceback (most recent call
last) in ()
1 for jew in range(0,Total_Jewels):
----> 2 Jewels[jew]=int(raw_input("Please Enter approx price for Jewel#"+str(jew+1)))NameError: name 'Jewels' is not defined
你不能这样填清单。因为您从一个空列表开始,
1 2 3 | jewels = [] for i in range(total_jewels): jewels.append(raw_input('Please Enter approx price for Jewel #{}: '.format(i))) |
或列表理解
1 | jewels = [raw_input('Please Enter approx price for Jewel #{}: '.format(i)) for i in range(total_jewels)] |
不知道你的代码是什么样子的,所以不知道你在问什么。
也许尝试一下:
1 2 3 | prices = [] for j in range(Total_Jewels): prices.append(input("Please enter approx price for Jewel#"+str(j+1))) |