A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 one year ago
Hi,
I'm following prof.Guttag's book, and i've gotten stuck at a simple exercise.
I have to add the decimal numbers in a string.But i keep getting an error, either "cannot convert string to float" when i try to exactly as the requirement.Or that the comma , is an "invalid literal" when i change everything to be an int.
What am i missing?
Thank you in advance for you're help ! :)
anonymous
 one year ago
Hi, I'm following prof.Guttag's book, and i've gotten stuck at a simple exercise. I have to add the decimal numbers in a string.But i keep getting an error, either "cannot convert string to float" when i try to exactly as the requirement.Or that the comma , is an "invalid literal" when i change everything to be an int. What am i missing? Thank you in advance for you're help ! :)

This Question is Closed

mathmate
 one year ago
Best ResponseYou've already chosen the best response.1It would help better if you could post an image of the question, and show what exactly you did.

rsmith6559
 one year ago
Best ResponseYou've already chosen the best response.0Both the float() and int() functions convert a number written as a string to a number. Your mention of commas makes me wonder if you have a list of strings that need to be converted one at a time.

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0Hi, Here is my 'code'. :). s = '1.23,2.4,3.123' total = 0.0 for c in s: total += float(c) print total

mathmate
 one year ago
Best ResponseYou've already chosen the best response.1as @rsmith6559 commented, strings representing single numbers will be converted automatically, but strings containing comma delimited values will not be converted automatically to an array. For what you need to do, you can define s as a list by s= [1.23,2.4,3.123] and the rest of the code should work.

mathmate
 one year ago
Best ResponseYou've already chosen the best response.1... except print total should read print(total)

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0Defining s as a list does work, however is there anyway for it to work keeping s='1.23,2.4,3.123' ? Just curious, seems strange because the book explicitly says to define s as such

mathmate
 one year ago
Best ResponseYou've already chosen the best response.1ok, that was an exercise to convert a string containing commadelimited numbers. So your program will require some string manipulations to do that. You will need to read it as a string and use the string functions (look up "split", for example) to make the list indirectly.

anonymous
 10 months ago
Best ResponseYou've already chosen the best response.0I had some trouble on this, and am quickly realizing that the book isn't going to always give you all the tools you need to solve the finger exercises right away. This is what I came up with s = str(raw_input('Enter a string of decimals seperated by commas: ')) total = 0 for i in s.split(','): total = total + float(i) print 'Sum of digits =', total
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.