2016-12-31 9 views
-2

私のコードでは、ユーザーがGTINコードの入力を検証しています。ユーザーが数字ではなく、テキストファイルではない数字を入力すると、Product Not Foundが出力されます私が数字以外の何かを入力すると、それはうまく動作します。しかし、0のような単一の数値を入力すると、完全に壊れて、Quantity is not definedと表示されますが、Quantityは私の変数の1つです。なぜそれが言われているのか分かりません。妥当性検査が壊れている場合があります

EDIT:0を入力すると、その出力Product Not Foundがエラーになります。

file = open("read_it.txt" , "r") 
for line in file: 
      line = line.strip('\n') 
      print(line) 
allprice=[] 
while True: 
    GTIN=(input("please enter GTIN for product or press enter for receipt")) 
    if(GTIN==''): 
     final = sum(allprice) 
     final=str(final) 
     print("total cost {0:33} £{1}".format("is",final)) 
     break 
    else: 
     if GTIN.isnumeric() and len(GTIN)==8 and GTIN in open('read_it.txt').read(): 
      while True: 
       Quantity="" 
       Quantity=(input("Please enter the Quantity")) 
       if Quantity.isdigit(): 
        break 
       else: 
        print("enter a number!") 
     else: 
      print("Product Not Found") 

     with open("read_it.txt", "r") as text_file: 
      for items in text_file: 
       line = items.strip('\r\n').split(",") 
       if GTIN in items: 
        product = line[1] 
        indprice = line[2] 
        finprice = float(indprice)* float(Quantity) 
        finprice=int(finprice) 
        print("{:<10} {:<10} {:<10} £{:<10} £{:<10}".format(GTIN,product,Quantity,indprice,finprice)) 

    allprice.append(finprice) 

答えて

0

ifブロック内で数量を定義しているようです。 "0"は8桁ではないので、Quantityは定義されず、float(Quantity)のためにエラーがスローされます。 ifブロックの外側でQuantityを定義することを検討してください。

+0

回答ありがとうございますが、私はそれを修正しました。これは、コードのtext_fileセクションが[Quantity]セクションでインデントされていないため、0を入力したときに量を求めずに計算を直接スキップするためです。新しいコードをチェックしてください。 –

関連する問題