How do I gather user numerical input into a list i

2020-02-07 13:40发布

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)

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.

2条回答
淡お忘
2楼-- · 2020-02-07 14:23

You can use this one line:

user_numbers = [int(num) for num in raw_input
                ("Enter number sequence separated by spaces: ").split()]

Notes:

查看更多
爷的心禁止访问
3楼-- · 2020-02-07 14:26

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()]

EDIT:

Duplicate of this question

查看更多
登录 后发表回答