anonymous
  • anonymous
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"
MIT 6.189 A Gentle Introduction to Programming Using Python (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
schrodinger
  • schrodinger
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
hey ,which language is this code ?
anonymous
  • anonymous
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?
anonymous
  • anonymous
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.

Looking for something else?

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

More answers

anonymous
  • anonymous
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??
anonymous
  • anonymous
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...
anonymous
  • anonymous
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"
wv12
  • wv12
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"
anonymous
  • anonymous
This works :D thanks!

Looking for something else?

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