Reputation: 23
I need to create a script to ask a user to input a string containing round numbers separated by a space. Then I need to convert the string to integers.
Any pointers on where to start? This is what I've tried:
a=str(input('Voer een door spatie gescheiden lijst met getallen in:').split())
b=int(a)
print(b)
but I keep getting:
ValueError: invalid literal for int() with base 10: "['50', '60']"
Upvotes: 1
Views: 1478
Reputation: 25093
Oversymplifyng the issue, the int
builtin (follow the link to know the whole story) requires a single argument, either a numeric literal or a string.
Your code
>>> a=str(input('Voer een door spatie gescheiden lijst met getallen in: ').split())
is creating the textual representation of a list, because you use the str
builtin that returns a textual representation of a python object:
>>> a
"['2', '3', '4', '5']"
The above is not the textual representation of a number, as required by int
, but the textual representation of a list, so when you pass it to int
it complains
ValueError: invalid literal for int() with base 10: "['2', '3', '4', '5']"
What if we omit the call to str
?
>>> a = input('Voer een door spatie gescheiden lijst met getallen in: ').split()
>>> a
['2', '3', '4', '5']
>>> int(a)
TypeError: int() argument must be a string or a number, not 'list'
Note that the error message is different: no more a ValueError
but a TypeError
. We have a list, whose items are strings representing numbers, but we're not allowed to use the list as is as an argument to int
.
Enter list comprehension, the contemporary idiom to manipulate the contents of a list (or of a generic iterable).
>>> [int(elt) for elt in a]
[2, 3, 4, 5]
The syntax is easy, [...]
you have the opening and closing bracket, as you would write when using a list literal, and inside you have an inside-out loop, first the body of the loop and then the for
specification. The results of the evaluation of the body are sequentially stored in a list, that the interpreter outputs for you, as you can see above we have no more strings representing numbers but integers. Assign the result of the list comprehension to a variable
>>> b = [int(elt) for elt in a]
and you're done:
>>> print(b)
[2, 3, 4, 5]
>>>
Upvotes: 1
Reputation: 180540
You are trying to cast a string representation of a list, just split the contents into a list and use map to cast to ints:
inp = input('Voer een door spatie gescheiden lijst met getallen in:').split())
a,b = map(int,inp)
This presumes the user always enters two numbers separated by a space.
Safer use a while loop and a try/except:
while True:
try:
inp = input('Voer een door spatie gescheiden lijst met getallen in:').split()
a, b = map(int, inp)
break
except ValueError:
print("Invalid input")
print(a,b)
If you have an unknown amount of ints entered:
while True:
try:
inp = input('Voer een door spatie gescheiden lijst met getallen in:').split()
nums = map(int, inp)
break
except ValueError:
print("Invalid input")
for num in nums:
print(num)
Using python3
, map returns a map object so it is an efficient way to assign.
[int(x) for x in inp]
will also work but unless you need a list there is no point creating one.
Upvotes: 3
Reputation: 10232
You need to convert each element of the list into int using the following way,
a = [int(each) for each in a]
This will give you a list of integers that you could use in your program. In case if it is an invalid integer, then you can use try-except
,
b = []
for each in a:
try:
b.append(int(each))
except ValueError:
print('Invalid number')
Upvotes: 0
Reputation: 118021
After split
you have a list
of str
, so you need to convert each of the individual items to int
.
You could use a list comprehension
b = [int(i) for i in a]
Or use map
b = list(map(int, a))
Upvotes: 0