您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用循环创建和分配多个变量(Python)

使用循环创建和分配多个变量(Python)

您可以一站式完成所有操作

how_many = int(input("How many terms are in the equation?"))

terms = {}

for i in range(how_many):
    var = int(input("Enter the coefficient for variable"))
    terms["T{}".format(i)] = var

然后您可以使用

 print( terms['T0'] )

但是使用列表而不是字典可能更好

how_many = int(input("How many terms are in the equation?"))

terms = [] # empty list

for i in range(how_many):
    var = int(input("Enter the coefficient for variable"))
    terms.append(var)

然后您可以使用

 print( terms[0] )

甚至(获得前三个学期)

 print( terms[0:3] )
python 2022/1/1 18:37:36 有224人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶