-1

I am trying to convert a user input of numbers to a list where I can find the mean of those numbers but I can't figure out a way to do it as I'm only a beginner so can anyone help me out? Thanks!

num_input = input("Enter your numbers here: ")

numbers = [num_input]

mean = sum(numbers)/len(numbers)
print(mean)
deceze
  • 510,633
  • 85
  • 743
  • 889
3lark
  • 1
  • Let the user separate the numbers by a specific character and then use `split()`. You also need to convert the texts to integer before summing up. – Thomas Weller Jun 09 '20 at 10:02
  • @ThomasWeller Oh ok Thanks! – 3lark Jun 09 '20 at 10:04
  • Perhaps easiest by example, using a list comprehension along the way: `numbers = [float(number) for number in num_input.split()]`. – 9769953 Jun 09 '20 at 10:05
  • 1
    FYI: `num_input` is a string. `[num_input]` turns this into a list with one string item in it, i.e. same as `['foobar']`. – deceze Jun 09 '20 at 10:05

2 Answers2

0

you can take the input string and use the builtin method strip and split and convert them to int objects. see here:

please refer to python docs for further reading in the link below:

str_methods

num_input = input("Enter your numbers here: ")

num_input=num_input.strip().split()
new_num_list = []
for num in num_input:
    new_num_list.append(int(num))

mean = sum(new_num_list)/len(new_num_list)
print(mean)

Take in mind that this code snippet could raise exceptions! maybe you want to except one of them annd do something with tha exception object

Adam
  • 2,820
  • 1
  • 13
  • 33
0

You can just split a string

num_input = input("Enter your numbers here: ")

numbers = [int(i) for i in num_input.split(',')]

mean = sum(numbers)/len(numbers)
print(mean)

Now you can pass in values like-

Enter your numbers here: 4, 7 , 4
5
Cool Developer
  • 408
  • 3
  • 12