2016-09-19 25 views
-1
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”对象不标化的,我不能工作了如何解决此问题,它曾经工作过。“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 == relooped 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") 

再次,它回答是,不管是什么输入。

对不起,我感谢任何帮助。

+0

你可以用'anythingelse.lower()更容易检查的情况下==“是”:'' – depperm

回答

4

为了您的循环if confirmation == ("YES") or ("Yes") or ("yes"):

这永远不会在Python工作,因为你基本上是问if confirmation is equal to "YES" or if ("Yes") or if ("yes");和if ("Yes")是真实的,因为它是有效的,而不是无或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) 
+0

如果confirmation.lower()==“是的“是一种更为Pythonic的比较方式。 – MattDMo

+0

@MattDMo我同意,但我的回答是指出解决他的问题,以便他可以从中学习。我会在后面添加你的答案。 – MooingRawr

0

(我猜你的评论,在注释行前用#)关于与“如果'比较问题,我建议你首先从字符串中删除Upper格式,然后进行比较。这将是随后更地道,并且还您选择的问题将得到解决:

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