[Tutor] Having Troubles with For Loops

jyatesster jyatesster at gmail.com
Sat Feb 19 19:06:47 CET 2011


I am refreshing my memory of Python programming after taking a class this
past fall. I am using the book, *Python Programming for the absolute
beginner*. I am at chapter 4, challenge 4 which instructs me to create a
program that picks a random word and the player has to guess the word. The
computer tells the player how mnay letters are in the word. Then the player
gets five chances to ask if a letter is in the word. The computer can only
respond with "yes" or "no." Then, the player must guess the word.

Here is what I have so far. I think I am doing something wrong with the for
loops as the program is not reading whether the letter is in the constant
and giving the appropriate response.

#Word Randomizer
#This program picks the word and the user has to figure out what the
computer
#picked. The computer tells how many letters are in the word. The player
#gets five chances to guess the correct word.
import random
# create a sequence of words to choose from
PYTHON = "python"
JUMBLES = "jumbles"
EASY = "easy"
DIFFICULT = "difficult"
XYLOPHONES = "xylophones"
WORDS = (PYTHON, JUMBLES, EASY, DIFFICULT, XYLOPHONES)

# pick one word randomly from the sequence
word = random.choice(WORDS)
# create a variable to use later to see if the guess is correct
correct = word
#intial values
tries = 1
max_tries = 5

#start the game
print(
"""
           Welcome to Word Randomizer!

   Guess the word the computer picks.
(Press the enter key at the prompt to quit.)
"""
)

#The computer picks the word and gives a hint
if correct == PYTHON:
    print ("This word has six letters.")
elif correct == JUMBLES:
    print ("This word has seven letters.")
elif correct == EASY:
    print ("This word has four letters.")
elif correct == DIFFICULT:
    print ("This word has nine letters.")
elif correct == XYLOPHONES:
    print ("This word has ten letters.")
else:
    raw_input("Press Enter to  Exit the Program.")
guess_letter = raw_input("Guess a letter: ")

#The player gets five chances to see if a letter is in the word.
for letter in PYTHON:
    if letter.lower() not in PYTHON:
        print ("No")
        guess_letter = raw_input("Guess a letter: ")
    else:
        print ("Yes")
        guess_letter = raw_input("Guess a letter :")
        tries +=1
    if tries == max_tries:
        guess_word = raw_input("Guess the word: ")

for letter in JUMBLES:
    if letter.lower() not in JUMBLES:
        print ("No")
        guess_letter = raw_input("Guess a letter: ")
    else:
        print ("Yes")
        guess_letter = raw_input("Guess a letter :")
        tries +=1
    if tries == max_tries:
        guess_word = raw_input("Guess the word: ")

for letter in EASY:
    if letter.lower() not in EASY:
        print ("No")
        guess_letter = raw_input("Guess a letter: ")
    else:
        print ("Yes")
        guess_letter = raw_input("Guess a letter :")
        tries +=1
    if tries == max_tries:
        guess_word = raw_input("Guess the word: ")

for letter in DIFFICULT:
    if letter.lower() not in DIFFICULT:
        print ("No")
        guess_letter = raw_input("Guess a letter: ")
    else:
        print ("Yes")
        guess_letter = raw_input("Guess a letter :")
        tries +=1
    if tries == max_tries:
        guess_word = raw_input("Guess the word: ")

for letter in XYLOPHONES:
    if letter.lower() not in XYLOPHONES:
        print ("No")
        guess_letter = raw_input("Guess a letter: ")
    else:
        print ("Yes")
        guess_letter = raw_input("Guess a letter :")
        tries +=1
    if tries == max_tries:
        guess_word = raw_input("Guess the word: ")
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110219/4d351a4d/attachment.html>


More information about the Tutor mailing list