如何获得多个输入Python

时间:2016-05-26 09:10:15

标签: python-3.x input

我正在用Python编写程序,我想要执行以下操作: 我要求写一些输入

x = int(input())

现在,给出数字' N'我分配给这个输入,然后我会得到N行要求新的输入。例如,如果我输入数字3,我希望程序要求3个新输入并将它们分配给某些字母:

x = int(input())
3

然后

a = input()
b = input()
c = input()

关于如何做到这一点的任何想法?

1 个答案:

答案 0 :(得分:0)

我建议您按照之前的回答,但如果您关心为用户的输入提供有意义的名称,您可以执行以下操作:

import sys
current_module = sys.modules[__name__] # you will have an access to module instance
print("Define number of further inputs:")
no = input()
for x in range(int(no)):
    print("Input value: ")
    _var = input()
    setattr(this_module, 'var_%s' % _var, _var)

之后,您可以通过下一个代码访问这些变量:

globals()[name_of_the_variable]

其中name_of_the_variable是' var _'加上用户的输入值,例如

Define number of further inputs: 5

var_5 = globals()['var_5']

希望这会对你有所帮助。