如何在Python中从用户那里获取多个输入

时间:2017-11-19 05:54:36

标签: python

我试图创建一个程序,其中python将接受多个输入,直到用户输入" END"。每个输入必须在不同的行上。我还是新手并且一般都在学习python /编程,所以有什么方法可以做到这一点吗?以及如何将输入转换为列表?

演示可能是什么样的 -

你有什么兴趣爱好?

远足

绘制

END

1 个答案:

答案 0 :(得分:0)

您可以使用input()功能阅读输入。你想要阅读直到结束'由用户输入。因此,请阅读输入并检查输入是否为“结束”。如果不是append它到列表中,则退出循环。

在代码中你可以这样做..

inputs = [] # list to  store the inputs

while True: # looping forever

    data = input('Enter the data : ') # read the data from user to variable data

    if data == 'END': # if END is read then exit the loop
        break

    else:
        inputs.append(data) # otherwise, append the input to the list

print(inputs) # display the list.

希望这会有所帮助。!!