A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 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 ! :)

  • This Question is Closed
  1. mathmate
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    It would help better if you could post an image of the question, and show what exactly you did.

  2. rsmith6559
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Both 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.

  3. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Hi, Here is my 'code'. :). s = '1.23,2.4,3.123' total = 0.0 for c in s: total += float(c) print total

  4. mathmate
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    as @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.

  5. mathmate
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    ... except print total should read print(total)

  6. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Defining 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

  7. mathmate
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    ok, that was an exercise to convert a string containing comma-delimited 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.

  8. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I 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

  9. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

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
  • 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.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.