【发布时间】:2016-08-23 12:10:14
【问题描述】:
我有一个问题,用户输入各种代码到 1 个变量中,然后我想将这些输入拆分成一个列表。但是,我意识到我的变量只接受最后一个输入,意思是列表出来只有一个代码,而不是几个。如何让变量存储多个输入?
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)
lst = GTIN.split()
print(lst)
我不能使用这个 (Two values from one input in python?) 因为我不知道用户想要多少项目,用户输入的项目可能从 1 到 5 不等。
【问题讨论】:
标签: python-3.x