1

I am very super new at Python and I've run into a problem. I'm trying to convert a string of numbers into an int or float so that I can add them up. This is my first question/post. Any suggestions are greatly appreciated!

total = 0
s = '2, 3.4, 5, 3, 6.2, 4, 7'

for i in s:
    total += int(i)
print total

I get the errors:

      *3 
      4 for i in s:
----> 5     total += int(i)
      6 print total

ValueError: invalid literal for int() with base 10: ','* 

5 Answers 5

5

You'll want to split the string at the commas using str.split. Then, convert them to floats (I don't know why you're using int when you say that you want to convert them to "an int or float").

total = 0
s = '2, 3.4, 5, 3, 6.2, 4, 7'
for i in s.split(','):
    total += float(i)
print total

Personally, I would prefer to do this with a generator expression:

s = '2, 3.4, 5, 3, 6.2, 4, 7'
total = sum(float(i) for i in s.split(','))
print total

The reason what you're doing doesn't work is that for i in s iterates over each individual character of s. So first it does total += int('2'), which works. But then it tries total += int(','), which obviously doesn't work.

Sign up to request clarification or add additional context in comments.

5 Comments

[comprehension -> generator expression]
@DSM Are they not also called "generator comprehensions"?
Not officially. Amusingly, I think Hettinger's original name was "generator comprehension", but Tim Peters suggested "generator expression" instead, and that's the one which was officially adopted. See the history in PEP 289.
@DSM Well, I'll be darned. Okay, changed.
FWIW, I often use replace(',', ' ').split() because it also allows the numbers in the string to simply be space-separated -- so a shorter ones like '2 3.4 5 3 6.2 4 7' and '2,3.4,5,3,6.2,4,7'also work.
3

You have a string of comma separated float values and not int. You need to split them first and then add them. You need to cast it to float and not int

total = 0
s = '2, 3.4, 5, 3, 6.2, 4, 7'

for i in s.split(','):
    total += float(i)
print total

Output will be 30.6

Comments

1

How about this? (^_^)

In[3]: s = '2, 3.4, 5, 3, 6.2, 4, 7'
In[4]: s = s.replace(",","+") # s = '2+ 3.4+ 5+ 3+ 6.2+ 4+ 7'
In[5]: total = eval(s)
In[6]: print(total)
30.6

3 Comments

Don't use eval .. It is very wrong to do so. See nedbatchelder.com/blog/201206/eval_really_is_dangerous.html . Use import ast; total = ast.literal_eval(s) instead
@BhargavRao: ast.literal_eval() won't evaluate the + operators. But you can use sum() on the output of evaluating s as literals: sum(ast.literal_eval(s)).
@MartijnPieters Thanks, for the insight. Hopefully liyuanhe211 will edit his answer.
0

You want to split the string

total = 0
for i in s.split(','):
    i = float(i) #using float because you don't only have integers
    total += i

Comments

0

Here is my approach.

total = 0
s = '2, 3.4, 5, 3, 6.2, 4, 7'
lst = s.split(',');
for i in lst:
    i = float(i)
    total += i
print total

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.