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

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

使用循环创建和分配多个变量(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 listfor i in range(how_many):    var = int(input("Enter the coefficient for variable"))    terms.append(var)

然后您可以使用

 print( terms[0] )

甚至(获得前三个学期)

 print( terms[0:3] )


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5644153.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-16
下一篇2022-12-16

发表评论

登录后才能评论

评论列表(0条)

    保存