0

I'm writing a hangman code, and am having trouble with setting an error for an invalid letter guess (ie. the guess should be between a-z, no numbers or multiple letter strings). This may be a fairly simple solution, but I am having trouble formalizing the code. Here is how I implemented the code:

while ((count < 6) and win):
   guess = input("Please enter the letter you guess: ")
   if guess:
      try:
         guess = guess.lower()
         lettersCorrect += 1
      if (guess not in letterList): #SYNTAX ERROR
         print ("You need to input a single alphabetic character!")
         continue

I noted where I am getting the syntax error. letterList is a list I created containing all acceptable letters (a-z). Is my coding off, or is there an easier way to say, "if not in letterList".

Thanks.

  • 1
    You have it correct, you just forgot and `except` block. But it is really superfluous to include a try. – squiguy Oct 26 '12 at 22:29

1 Answers1

0

Your try block is pointless. Also, instead of letterList, you can use string.lowercase

import string

while count < 6 and win:
   guess = input("Please enter the letter you guess: ")
   if guess:
      guess = guess.lower()
      if guess not in string.lowercase:
         print("You need to input a single alphabetic character!")
         continue
      lettersCorrect += 1
Eric
  • 95,302
  • 53
  • 242
  • 374