如何在Python中将用户数字输入收集到列表中? [英] How do I gather user numerical input into a list in Python?
本文介绍了如何在Python中将用户数字输入收集到列表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Python的新手,正在尝试制作一个简单的程序来根据用户输入的数字计算平均中位数和众数.到目前为止,我有:
I'm new to Python and am trying to make a simple program to calculate mean median and mode from numbers input by user. So far I have:
num=[]
UserNumbers=int(input("Enter number sequence separated by spaces: "))
num.append(UserNumbers)
print (num)
我希望用户能够输入多个用空格分隔的int,但是我的代码仅接受一个数字.平均值/中位数/众数部分应该不难,因为我将在3.4中使用统计数据包.只需要帮助收集输入即可.
I want the user to be able to input multiple int's separated by spaces, however my code only accepts one number. The mean/median/mode part shouldn't be hard as I'm just going to use statistics package in 3.4; just need help with gathering input.
推荐答案
如果要这样,就必须解析答案.
You have to parse the answer if you want it this way.
UserNumbers=input("Enter number sequence separated by spaces: ")
nums = [int(i) for i in UserNumbers.split()]
这篇关于如何在Python中将用户数字输入收集到列表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文