How can I store the input data into a list using Python?
In Python, you can use the input() function to get user input and the append() method to add it to a list.
Here is an example code:
my_list = [] # 创建一个空列表
# 循环获取用户输入,直到输入"exit"为止
while True:
data = input("请输入数据(输入'exit'退出):")
if data == "exit":
break
my_list.append(data) # 将数据添加到列表中
print("输入的数据为:", my_list)
After running the above code, the program will loop and wait for user input data until the user enters “exit”. Each data input by the user will be added to the my_list list. Finally, the program will output all the inputted data.
Example execution output:
请输入数据(输入'exit'退出):123
请输入数据(输入'exit'退出):abc
请输入数据(输入'exit'退出):exit
输入的数据为: ['123', 'abc']