[Tutor] Having Troubles with For Loops

delegbede at dudupay.com delegbede at dudupay.com
Sat Feb 19 20:33:54 CET 2011


To start with, you don't need to do so much for loop. 
You have already set the chosen word, word to correct. 
Every randomly selected word becomes the value of correct so, you need to only check for word in correct.
So, after initiating the random selector, you print the number of letters in the word using the syntax like this:
The player has 5 tries, depending on the approach you intend to adopt, you may not have to use max_tries. 
Say you go for the while loop. 
Set tries to 1
correct=word[random.randrange(len(word)-1)]
word = a list of words you want the player to choose from
print 'The word has %d letters' % len(correct)  
while tries <=  5:
    guess=raw_input('Guess a letter: ')
    if guess in correct:
        print 'yes'
    else:
        print 'no'
    tries += 1

What you would achieve with the above is that, immediately the player does 5 guesses, the game exits.  How do you expect a player to guess a 7 letter word with just 5 tries. 
Second, the player should after 5 tries, if you insist on that, have a chance to guess the word itself assuming he has he has made two or three guesses, atleast give him a chance to make a wild guess. You can then tell him if he is right or wrong and go ahead to tell him the word. 
This is just a game logic from my own perspective. 
Do the necessary tweak to the cold. If there are other problems, please let's know. 
I hope this helps. 
Regards. 

Sent from my BlackBerry wireless device from MTN

-----Original Message-----
From: Corey Richardson <kb1pkl at aim.com>
Sender: tutor-bounces+delegbede=dudupay.com at python.org
Date: Sat, 19 Feb 2011 13:22:25 
To: <tutor at python.org>
Subject: Re: [Tutor] Having Troubles with For Loops

On 02/19/2011 01:06 PM, jyatesster wrote:
> 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.
>  [snip]

You're going about it wrong. You should look into the len() function.
For example:

print("This word has %d letters" % (len(word)) )
for i in range(5):
    letter = input("Guess a letter: ")
    if letter.lower() in word.lower():
        print("Yes")
    else:
        print("No")

Your for loops in the end don't do what you think they do.

for letter in XYLOPHONES: # Loops through XYLOPHONES
    if letter.lower() not in XYLOPHONES: # Always will return false,
        print("No")    # because you are looping through the word itself
        # etc.

You are also mixing Python 3's print() with Python 2's raw_input(), so
it's hard to tell which you are using. I'm assuming Python 2 because you
didn't report the raw_input() failing. If that's the case, you don't
need the ()'s around the text you are printing.

I also suggest you look into lists and list indexing. You should go
through the Python Tutorial [0] if you haven't before.

[0] - http://docs.python.org/tutorial/
-- 
Corey Richardson

I've never known any trouble which an hour's
reading didn't assuage.
-Charles De Secondat
_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list