我将使用collections库中的defaultdict来完成该任务。这将确保每个输入变量的索引位置。
使用索引和变量,您可以完成其余的工作。在from collections import defaultdict
in_number = defaultdict(list)
loop = True
counter = 1
while loop:
try:
number = int(input("Enter number: "))
in_number[counter] = number
counter+=1
# catch errors
except Exception as e:
# find better way to deal with them
print(e)
continue_ = input("Want to continue? ")
if continue_.lower() == 'y':
loop = True
else:
loop = False
print('First number',in_number[1])
if len(in_number)>1:
print('Second number +1', in_number[2]+1)
所以,我们使用while循环来填充用户输入的数字。每个数字都与其索引一起存储。我们捕捉到如果数字输入不是浮点或整数将引发的错误。我们要求继续并修改循环变量。最好的是你可以添加更多的继续选项。E、 g
^{pr2}$
希望这能给你指路;)