ProTechXS ProTechXS -4 years ago 173
Python Question

How do I split integers in a list?

So I may sound like a noob but I wan't to know how I can split integers in a list
however the list is kept in a variable where the user can input his own numbers
currently this is what I have
Please spot if I have made any mistakes

num = []
item = input(str("Please enter a series of numbers separated by a space").split())
[int(digit) for digit in str(item)]
num.append(item)
print(num)

quit_program = input("Would you like to print the average out or quit the program?")
#Type "average" for the average however if you want to quit then type in "quit"

if quit_program == "quit":
quit()
elif quit_program == "average":
sum(num)/len(num)

Answer Source

If I understand what you're trying to do, you've got your split in the wrong place. You want to split the input string, not your prompt.

>>> inp = raw_input("please enter a series of numbers separated by a space\n")
please enter a series of numbers separated by a space 
3 4 5
>>> nums = [ int(i) for i in inp.split()]
>>> nums
[3, 4, 5]

You're also going to want to use raw_input if you just want your input to be a space separated string (see here: http://www.python-course.eu/input.php). Then you're going to want to assign that conversion to ints somewhere so that you can use it later for you averaging.

Hope this helps!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download