Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

AlexZet

Hi, I would like to ask a question, I'm trying to create a while loop, I'm trying to make the program to recognize string characters, but when finally I found a way to do it now it doesn't recognize negative numbers. Here´s the code, hopefully with some feedback I can correct the issue. r = raw_input ("enter a number:") if r.isdigit() and int(r): print r while r != 0: print r if r > 0: r = int(r)-1 elif r < 0: r = int(r)+1 else: print "enter a valid numeric value"

  • one year ago
  • one year ago

  • This Question is Closed
  1. ayoup
    Best Response
    You've already chosen the best response.
    Medals 0

    hey ,which language is this code ?

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

    AlexZet: Try adding a test line right after the raw_input print r.isdigit() Enter a negative number. What do you get for True/False from the test line? How is the minus sign being seen?

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

    Hi, ayoup the language is python, algotruneman, l did what you tell me and I'm getting False with negatives, I don't understand why.

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

    xD I'm reading right now and this functions doesn't allow floats or rational numbers, so do you have any other advise to filter string from this program??

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

    I think AlexZet is trying to point out that a minus sign in raw_input is 'seen' as a string. type(r) gives string. So, I'm thinking to convert to float directly after input...

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

    Yes, you are right, that is the way that a minus sign is seen by that function, you can use just the "input" and python will recognize positive and negative numbers but if I enter a string it will go into the "if" statement and them to the elif due the conditions, then it will try to add one to the variable and it crash, this is what I cannot resolve, sorry for the long answer but maybe I explained myself better this time. This is the code so you can see what I talking about: r = input("enter a number:") while r != 0: print r if r > 0: r = r-1 elif r < 0: r = r+1 else: print "enter a valid numeric value"

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

    when using raw_input(), python always returns a string. You can check wether the answer is an integer using try: try: s = int(r) except ValueError: print "this is not an integer"

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

    This works :D thanks!

    • one year ago
    • Attachments:

See more questions >>>

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.