Python “int”对象不可下标错误
这里出现的错误是,在GTIN0=GTIN[0]等行上,“int”对象不可下标,我无法解决如何修复它,它以前是可以工作的 这里是list2.txt,仅供参考Python “int”对象不可下标错误,python,python-3.x,csv,Python,Python 3.x,Csv,这里出现的错误是,在GTIN0=GTIN[0]等行上,“int”对象不可下标,我无法解决如何修复它,它以前是可以工作的 这里是list2.txt,仅供参考 while 1 == 1: import csv from time import sleep import sys bill = 0 with open('list2.txt') as csvfile: readCSV = csv.reader(csvfile, delimit
while 1 == 1:
import csv
from time import sleep
import sys
bill = 0
with open('list2.txt') as csvfile:
readCSV = csv.reader(csvfile, delimiter = ",")
GTINs = []
products = []
prices = []
for row in readCSV:
GTIN = row[0]
product = str(row[1])
price = float(row[2])
GTINs.append(GTIN)
products.append(product)
prices.append(price)
x = 1
print("Welcome to the GTIN shop!")
while x == 1:
try:
sleep(1)
print("Please input the 8 digit GTIN code")
sleep(0.5)
GTIN0 = GTIN[0]
GTIN1 = GTIN[1]
GTIN2 = GTIN[2]
GTIN3 = GTIN[3]
GTINx = input("--> ")
下一个错误出现在这里,从最后一段继续:
45112454,Milk,1.29
55555555,Bread,0.49
87595376,Milkshake,1.99
要检索牛奶,代码是7。面包的代码是8,奶昔的代码是9。我不知道python是从哪里得到这些数字的
GTINx = input("--> ")
if GTINx == GTIN0:
product1 = products[0]
price1 = prices[0]
x = 2
elif GTINx == GTIN1:
product1 = products[1]
price1 = prices[1]
x = 2
elif GTINx == GTIN2:
product1 = products[2]
price1 = prices[2]
x = 2
elif GTINx == GTIN3: (this one is just here for if another one is added)
product1 = products[3]
price1 = prices[3]
x = 2
else:
print("Have another go")
except:
print("ERROR - Try Again")
因此,这应该结束循环,如果他们说不,则返回开始,而1==1:但是,不管键入什么,这就像他们说是一样
接下来是一个类似的问题
while x == 3:
try:
sleep(1)
print("So you would like", number, product1, "?")
sleep(0.5)
confirmation = input("Please enter \"YES\" or \"NO\": --> ")
if confirmation == ("YES") or ("Yes") or ("yes"):
x = 4
elif confirmation == ("NO") or ("No") or ("no"):
x = 1
else:
sleep(0.5)
print("Have another go")
except:
print("ERROR - Try Again")
同样,无论输入什么,它都回答是
很抱歉收到垃圾邮件,感谢您对我的帮助。如果确认==是或是或是:
这在Python中永远不会起作用,因为您基本上是在询问确认是否等于YES,或者确认是否等于YES,或者确认是否等于YES;如果是,则为真,因为它是有效的,而不是无、0或空。你想要:
while x == 4:
try:
cost = price1 * number
bill = bill + cost
print("The cost is", cost)
sleep(5)
print("Would you like to purchase anything else?")
sleep(0.5)
anythingelse = input("Please enter \"YES\" or \"NO\": --> ")
sleep(1)
if anythingelse == ("YES") or ("Yes") or ("yes"):
x = 1
elif anythingelse == ("NO") or ("No") or ("no"):
x = 5
else:
sleep(0.5)
print("Have another go")
except:
print("ERROR - Try Again")
有其他的方法来做你的或检查,但我只是要纠正你有什么
正如评论和其他答案所指出的,检查“是”的更好方法是:
if confirmation == ("YES") or confirmation == ("Yes") or confirmation == ("yes"):
只需将输入转换为小写并检查值
至于你的第一期。GTIN0=GTIN[0]你是说GTIN0=GTINs[0]。因为GTIN是一个int,而不是像错误告诉你的那样可下标的东西
至于GTIN0的第二个问题以及其他问题,请查看修复程序是否为您修复了它,因为GTIN0等从未正确设置。如果修复后问题仍然错误,请编辑问题
还有这条线
if confirmation.lower() == "yes"
不是真的正确的评论我猜你的评论,使用前面的评论行
elif GTINx == GTIN3: (this one is just here for if another one is added)
关于“if”比较问题,我建议您首先从字符串中删除大写格式,然后进行比较。这样会更加地道,你的问题也会得到解决:
elif GTINx == GTIN3: #(this one is just here for if another one is added)
你可以用任何东西来更容易地检查案例。lower=='yes':如果确认。lower==yes是一种更像蟒蛇的比较方式。@MattDMo我同意,但我的回答是为了解决他的问题,以便他可以从中学习。我想稍后我会把你的答案加进去。
if anythingelse.lower() == ("yes"):
x = 1
elif anythingelse.lower() == ("no"):
x = 5
else:
sleep(0.5)
print("Have another go")