2016-09-19 22 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") 
相關問題