0

I'm writting a piece of program in which I ask for input from user.

I want python to check if the input is digit (not words or puntuation...) and if it is a number indicating an object in my tuple. If one of the 3 conditions result in False then I would like the user to provide another value for that variable. Here's my code

colour={'yello':1, 'blue':2, 'red':3, 'black': 4, 'white': 5, 'green': 6}
height_measurements=('centimeter:1', 'inch:2', 'meter:3', 'foot:4')
weight_measurements=('gram:1', 'kilogram:2', 'pounds:3')
print height_measurements
hm_choice = raw_input('choose your height measurement').lower()
while not hm_choice.isdigit() or hm_choice > 0 or hm_choice < len(height_measurements) :
    hm_choice = raw_input('choose your height measurement').lower()        
print weight_measurements
wm_choice = raw_input('choose your weight measurement').lower()
while not wm_choice.isdigit() or wm_choice > 0 or wm_choce < len(weight_measurements) :
    wm_choice = raw_input('choose your weight measurement').lower()

When I put this to test, it kept making me insert input for height_measurement constantly no matter what I put in

Please check my code and correct for me. Also if you will, please provide me with better code of yours.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
ttriet204
  • 353
  • 1
  • 5
  • 13

2 Answers2

6

I won't fix your code for you entirely, but I will explain something to you which you seem to be confused about.

raw_input returns a string. Strings and integers are two types and cannot be compared to each other (even though in python 2 this is does not raise a TypeError). So your variable hm_choice is a string and you're correct to use the isdigit method to ensure it is an integer. However, you're then comparing a string to an integer which will always evaluate to True in one of those conditions which means that while loop will never stop. So I pose this question to you: How do you get an integer from a string?

Next, you need to examine the logic of that loop. You're saying: While hm_choice is not a digit OR while hm_choice is greater than 0 (which we already know is an invalid statement) OR while hm_choice is less than 4 (or the length of your tuple).

So if any of those are True, then the loop will not end. If you read the article I linked above, you'll figure out which of those always evaluates to True. ;)

Ian Stapleton Cordasco
  • 26,944
  • 4
  • 67
  • 72
  • THANK YOU SO MUCH! I profitted alot from your answer. What you wrote there was just exactly what happened! I now understand why the program kept asking for input. However, I'm still working on the question you posed to me "how to get an integer from a string". I had thought that when user puts in a number then Python shall take that as a numeric object and so provide methods as well as operators that work on number. – ttriet204 Jan 08 '13 at 12:05
  • Since you seem stuck, if you look at @jary's answer you'll see that he takes `hm_choice` and calls `int(hm_choice)` assuming `hm_choice` is an integer (you guarantee that by placing `hm_choice.isdigit()` first) it will return an integer for you. – Ian Stapleton Cordasco Jan 08 '13 at 15:15
0

When I put this to test, it kept making me insert input for height_measurement constantly no matter what I put in

That was because hm_choice > 0 is comparision between string and int, which is undefined and can equal either True or False depending on the implementation.

I didn't quite get the meaning of the third condition, so i just placed the THE_OTHER_CONDITION there instead. If you define THE_OTHER_CONDITION = True the code will work.

colour={'yello':1, 'blue':2, 'red':3, 'black': 4, 'white': 5, 'green': 6}
height_measurements=('centimeter:1', 'inch:2', 'meter:3', 'foot:4')
weight_measurements=('gram:1', 'kilogram:2', 'pounds:3')

print height_measurements
while True:
    hm_choice = raw_input('choose your height measurement: ').lower()
    if (hm_choice.isdigit() and int(hm_choice) > 0 and THE_OTHER_CONDITION):
        break

print weight_measurements
while True:
    wm_choice = raw_input('choose your weight measurement: ').lower()
    if (wm_choice.isdigit() and int(hm_choice > 0) and THE_OTHER_CONDITION):
        break
Community
  • 1
  • 1
jary
  • 1,161
  • 6
  • 9
  • Thank you! This is very helpful to me! However, I haven't known what the "break" code is for. I'm looking it up right now. – ttriet204 Jan 08 '13 at 12:13
  • Break jumps out of the infinite while loop. It is classical [pattern to create do-until loop in Python](http://stackoverflow.com/a/1662176/1950100) – jary Jan 08 '13 at 12:44