I'm working on this coding puzzle and have to covert some numbers in a string to integers to work with them. an example would be
('2 -5 7 8 10 -205')
What I tried to do was add the numbers to an empty string and convert them to an int when there was a space. Here's the code.
n is the length of the string of numbers num is the empty string I add the numbers to. Originally num=""
while i<n:
if temps[i]!=' ':
num=num+temps[i]
elif temps[i]==' ':
print type(num)
x=int(num)
The problem is that when it runs I get an error for the line with x=int(num) saying
ValueError: invalid literal for int() with base 10: ''
when i print num I just get numbers in a string format, so I don't understand what's wrong. Help would be really appreciated, and if you have any questions or need clarification please ask.
Thanks