'int'对象不可订阅错误

时间:2016-09-19 13:42:04

标签: python python-3.x csv

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("--> ")

这里出现的错误是GTIN0 = GTIN [0]等行,'int'对象不可订阅,我无法弄清楚如何修复它,以前它曾经工作过。

供参考,这里是“list2.txt”。

45112454,Milk,1.29
55555555,Bread,0.49
87595376,Milkshake,1.99

此处出现下一个错误(从最后一段继续):

            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")

要检索牛奶,代码是7.对于面包,代码是8,对于奶昔,代码是9.我不知道python从哪里得到这些数字...

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")

所以,这应该结束那个循环,并且如果他们说不,则将它们发送回开始(在1 == 1时重新循环)但是,无论输入什么,它都表现得好像是肯定的。

接下来是一个类似的问题......

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")

同样,无论输入什么,它都回答是。

对不起垃圾邮件,感谢您提供的任何帮助。

2 个答案:

答案 0 :(得分:4)

对于你的循环if confirmation == ("YES") or ("Yes") or ("yes"):

这在Python中永远不会起作用,因为你基本上是在问if confirmation is equal to "YES" or if ("Yes") or if ("yes");并且if ("Yes")为真,因为它是有效的而不是None或0或空。你想要:

if confirmation == ("YES") or confirmation == ("Yes") or confirmation == ("yes"): 

还有其他方法可以进行or检查,但我只是想纠正你的问题。

正如评论和其他答案所指出的,检查“是”的更好方法是:

if confirmation.lower() == "yes"

只需将输入转为小写并检查值。

关于你的第一期。 GTIN0 = GTIN[0]您的意思是GTIN0 = GTINs[0]。因为GTIN是一个int而不是“可订阅”的东西,就像错误告诉你的那样。

至于您使用GTIN0的第二个问题以及不存在的问题,请查看修复程序是否因为GTIN0等问题而修复了它,因此从未正确设置。如果修复后仍然出现问题,请编辑您的问题。

也是这一行

elif GTINx == GTIN3: (this one is just here for if another one is added)

评论不正确(我猜测你的评论,在评论行前面使用#

elif GTINx == GTIN3: #(this one is just here for if another one is added)

答案 1 :(得分:0)

关于' if'比较问题,我建议你首先从字符串中删除Upper格式然后比较。那将更加惯用,你的问题也将得到解决:

if anythingelse.lower() == ("yes"):
        x = 1 
elif anythingelse.lower() == ("no"):
        x = 5 
else:
        sleep(0.5)
        print("Have another go")