while n == 1:
w = input("Input the product code: ")
问题在于当输入w的新代码时,w会被覆盖。例如,如果w = 24,然后w = 54,如果您打印w,它只会打印54,因为这是最新的输入。如何使其打印出w的所有输入?
while n == 1:
w = input("Input the product code: ")
问题在于当输入w的新代码时,w会被覆盖。例如,如果w = 24,然后w = 54,如果您打印w,它只会打印54,因为这是最新的输入。如何使其打印出w的所有输入?
inputs = []
for i in range(expected_number_of_inputs):
inputs.append(input('Product Code: '))
for i in inputs:
print(i)
使用容器类型代替单个变量。在这种情况下,list()
似乎是合适的:
inputs = [] # use a list to add each input value to
while n == 1:
inputs.append(input("Input the product code: ")) # each time the user inputs a string, added it to the inputs list
for i in inputs: # for each item in the inputs list
print(i) # print the item
注意:上述代码将无法编译。您需要为变量n
填充值。
w1 = input("value1")
w2 = input("value2")
print(w1)
print(w2)
解决方案2:使用列表
w = []
w.append(input("value1"))
w.append(input("value2"))
print(w[0])
print(w[1])
w
,然后只需将每个条目附加到其中? - idjaw