Reputation: 187
I am making an nth term calculator and I want the user to enter any sequence they like.
Say if they put: 12, 16, 20, 24
, it will be stored in a list like [12, 16, 20, 24]
How can I do this?
Upvotes: 0
Views: 60
Reputation: 9347
If you are getting the input as a string, you can just do the following: map(lambda x: int(x), "12, 16, 20, 24".split(","))
More generally, you would want to do:
nums = raw_input()
map(int, nums.split(","))
Upvotes: 1
Reputation: 5629
The simplest way is to use the split method:
map(int, raw_input().split(","))
Make sure to split only by ","
not by ", "
, because the latter will raise a ValueError for an input like "12,14,17"
:
Upvotes: 0
Reputation: 8558
Here's a list comprehension method:
in_str = "1, 2, 3, 4"
listed = [int(x) for x in in_str.split(',')]
Upvotes: 2