在我的代码中,根据用户输入的号码,它会询问用户"请输入所需产品的GTIN代码"。 因此,如果用户最初输入5,则询问问题5次。 这意味着我有多个输入用于变量" GTIN" ,所以在我的代码结束时,我需要在向下列表中打印出所有代码,但如果我尝试打印GTIN,它只会粘贴用户的最后一个输入。
我的代码:
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")
print(GTIN)
` 使用代码 46389121测试时,您只能输入最多5个项目。