如何创建通过循环的新变量?

时间:2019-02-21 17:34:42

标签: python loops variables input

我希望能够在循环中使用新的变量输入创建新的用户输入。

即假设x为7(7来自用户输入),我希望它输出:

y1 = int(input("What is the x of component y1? "))
y2 = int(input("What is the x of component y2? "))
y3 = int(input("What is the x of component y3? "))
y4 = int(input("What is the x of component y4? "))
y5 = int(input("What is the x of component y5? "))
y6 = int(input("What is the x of component y6? "))
y7 = int(input("What is the x of component y7? "))

尽管我确实进行了尝试研究,但并未找到与之链接的另一篇文章,而且对于解决该问题确实很有用,谢谢,万一其他人看到了,我在下面添加了解决方案:

for i in range(1, 8):
    globals()['y{}'.format(i)] = float(input("What is the x of component y{}? ".format(i)))

0 个答案:

没有答案