0

I'm working on a Hangman game in Python and I need user validation for the input, which I have made an attempt at, but I don't know why its not working.

My task was to have "error" messages for 1. empty input, 2. non-integer, non-empty input, 3. index out of range input. By index out of range, I mean that I am asking the user for an integer from 0-9 to select a word from the list already in the program.

def getLetterFromUser(totalGuesses):

  while True:
      userInput = input("\nPlease enter the letter you guess:")
      if userInput == '' or userInput == ' ':
          print("Empty input.")
      elif userInput in totalGuesses:
          print("You have already guessed that letter. Try again.")
      elif userInput not in 'abcdefghijklmnopqrstuvwxyz':
          print("You must enter an alphabetic character.")
      else:
          return userInput

For clarity's sake, the subsequent call to getLetterFromUser is in a while loop so that it repeatedly checks these conditions.

EDIT: I took out what didn't belong. Thank you. My problem, however, is that it still tells me the input isn't in alphabet, when it is. And the length of the input(a single char) is 2, which doesn't make sense unless it counts the null character.

trainreq
  • 381
  • 4
  • 12
  • 1
    What problem or error are you experiencing? – YXD Feb 15 '12 at 00:44
  • Why are you having the user select the word, and why is that option in the "Guess a letter" prompt loop? – Edwin Feb 15 '12 at 00:46
  • @Edwin My apologies, that if got pasted in there unknowingly. Disregard it, as it goes elsewhere. To others: the error I'm receiving is that no matter what I input, its never right. It usually says "You must enter an alphabetic character", to which I do. Also, I was curious so I checked the length of the input(a single character) and its ALWAYS 2. I don't know why. – trainreq Feb 15 '12 at 00:48
  • Your question seems related to [How to handle both integer and string from raw_input?](http://stackoverflow.com/q/8961627/1132524) take look at it :) *(obviously what was raw_input in python-2.x is now input in python-3.x)* – Rik Poggi Feb 15 '12 at 00:55
  • In response to your edit, try `print(repr(userInput))` to see what's *really* in there (it won't be a NUL character, those don't work the same way in Python as they do in C - in Python NUL is just another perfectly valid character). – Greg Hewgill Feb 15 '12 at 03:18

4 Answers4

1

your problem is that some validation rules should take precedence over others. for example, if userInput is an empty string, what do you expect userInput < 0 to return ? what if it's not empty but also not a number ?

think about which conditions should be checked first. some functions you might want to read about and use:

"123".isdigit() # checks if a string represents an integer number
" 123 ".strip() # removes whitespaces at the beginning and end.
len("") # returns the length of a string
int("123") # converts a string to an int
yurib
  • 8,043
  • 3
  • 30
  • 55
0

Here are two things to start with:

what is the purpose of the line

userInput = userInput.lower()

If you are assuming the userInput is an integer.. You should try userInput=int(userInput). Integers have no .lower() method.

the next line

if 0 > userInput or userInput > 9

this assumes userInput is an integer (you are comparing to 0 and 9, not "0" and "9")

the following looks nicer:

if not 0<=userInput<=9
Rusty Rob
  • 16,489
  • 8
  • 100
  • 116
0

You say you want integer answers, but you are not casting the input to an int, but then you say that if the input is not in the alphabet, it should return an error message. You're asking for two different things.

Do you want the user to enter an integer or a character?

purpleladydragons
  • 1,305
  • 3
  • 12
  • 28
0

This may help you:

>>> int(" 33   \n")
33
>>> int(" 33a asfd")
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '33a asfd'
>>> try:
...     int("adsf")
... except ValueError:
...     print "invalid input is not a number"
...     
invalid input is not a number
Rusty Rob
  • 16,489
  • 8
  • 100
  • 116
  • I was in pyscripter which is an IDE for python. You just press enter to go to the next line. http://stackoverflow.com/questions/81584/what-ide-to-use-for-python – Rusty Rob Feb 15 '12 at 01:17