我正在制作一个程序,询问用户他们想要多少件物品。用户最多可以输入5.我还创建了一个包含5个项目的文本文件,其中包含唯一的代码,价格和产品名称。
让我们说用户输入3,它会提示用户输入一个代码,分3次。之后,它将询问用户想要的每个项目的数量。我的程序然后打印出代码,产品名称,价格,所需数量和总价格。
现在这就是我遇到的问题:我的代码只对最后给出的项目执行此操作。如果我输入鸡肉的代码,它只会输出鸡肉清单,而不是之前的其他产品。
代码:
while True:
itemsneeded = input("How many items do you need?")
if itemsneeded.isnumeric() and int(itemsneeded) <= 5:
break
GTIN=''
count=0
while count<int(itemsneeded):
GTIN=(input('Please enter all GTIN-8 for all items'))
if GTIN.isnumeric() and len(GTIN)==8:
Num0=int(GTIN[0])*3
Num1=int(GTIN[1])
Num2=int(GTIN[2])*3
Num3=int(GTIN[3])
Num4=int(GTIN[4])*3
Num5=int(GTIN[5])
Num6=int(GTIN[6])*3
Num7=int(GTIN[7])
total2=(Num0+Num1+Num2+Num3+Num4+Num5+Num6+Num7)
if total2 % 10 == 0:
print(GTIN)
if GTIN in open('read_it.txt').read():
print('entered GTIN is valid')
else:
print('The code entered is invalid')
print('Please renter this code')
count+=1
else:
print("The entered GTIN-8 codes are incorrect")
count=0
while count<int(itemsneeded):
quantity=input('Please enter the Quantity needed:')
if quantity.isnumeric() and int(quantity)<10:
count+=1
else:
break
从这里开始它只是一团糟,我试图根据给出的代码打印出文本文件中的项目:
if GTIN=='86947367':
with open("read_it.txt") as fp:
next(fp)
next(fp)
total1=int(quantity)*int(next(fp))
if GTIN=='86947367':
with open("read_it.txt") as fp:
next(fp)
print('86947367'.rstrip(),next(fp).rstrip(),quantity.rstrip(),next(fp).rstrip(),'£',total1)
if GTIN=='78364721':
with open("read_it.txt") as fp:
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
total1=int(quantity)*int(next(fp))
if GTIN=='78364721':
with open("read_it.txt") as fp:
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
print('78364721'.rstrip(),next(fp).rstrip(),quantity.rstrip(),next(fp).rstrip(),'£',total1)
if GTIN=='35619833':
with open("read_it.txt") as fp:
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
total1=int(quantity)*int(next(fp))
if GTIN=='35619833':
with open("read_it.txt") as fp:
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
print('35619833'.rstrip(),next(fp).rstrip(),quantity.rstrip(),next(fp).rstrip(),'£',total1)
if GTIN=='84716491':
with open("read_it.txt") as fp:
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
total1=int(quantity)*int(next(fp))
if GTIN=='84716491':
with open("read_it.txt") as fp:
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
print('84716491'.rstrip(),next(fp).rstrip(),quantity.rstrip(),next(fp).rstrip(),'£',total1)
if GTIN=='46389121':
with open("read_it.txt") as fp:
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
total1=int(quantity)*int(next(fp))
if GTIN=='46389121':
with open("read_it.txt") as fp:
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
next(fp)
print('46389121'.rstrip(),next(fp).rstrip(),quantity.rstrip(),next(fp).rstrip(),'£',total1)
我想知道如何处理多个用户输入并使用它们从文本文件中输出所需的信息。
文字文件:https://mega.nz/#!GQ0BhTKD!KsC4ZhFW7LJuwXXkJFyjdS9geiiJjaoUy6CAbuD6qkI