User input being limited to the alphabet in python

Posted by Danger Cat on Stack Overflow See other posts from Stack Overflow or by Danger Cat
Published on 2013-06-26T22:14:23Z Indexed on 2013/06/26 22:21 UTC
Read the original article Hit count: 155

Filed under:

I am SUPER new to programming and have my first assignment coming up in python. I am writing a hangman type game, where users are required to guess the word inputted from the other user.

I have written most of the code, but the only problem I am having is when users have to input the word, making sure it is only limited to the alphabet. The code I have so far is :

word = str.lower(raw_input("Type in your secret word! Shhhh... "))

answer = True

while answer == True:
    for i in range(len(word)):
         if word[i] not in ("abcdefghijklmnopqrstuvwxyz"):
             word = raw_input("Sorry, words only contain letters. Please enter a word ")
             break

         else:
             answer = False

This works while I input a few tries, but eventually will either exit the loop or displays an error. Is there any easier way to use this? We've really only covered topics up to loops in class, and break and continue are also very new to me. Thank you! (Pardon if the code is sloppy, but as I said I am very new to this....)

© Stack Overflow or respective owner

Related posts about python