python变量名称跟着循环_python在循环中存储每次迭代使用不同名称的输入变量

我将使用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}$

希望这能给你指路;)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值