[Tutor] Q

Brett Ritter swiftone at swiftone.org
Thu Jun 9 02:23:58 CEST 2011


On Wed, Jun 8, 2011 at 3:25 PM, Vincent Balmori
<vincentbalmori at yahoo.com> wrote:
> In the "Loop of Game" section of my code for some reason it gives me five
> more chances than I wanted it to. When I put two as the chance limit, it
> allowed seven. Also, the program will always say "yes" to any letter I
> enter, even if it's wrong.

Let's take a look. I'll comment on a few parts that aren't the cause
of your problems.

> WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone")
> # 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

I'm unclear on the purpose of this step.  You never again use word, so
why have correct and not just use word?

> LETTERS = None
> if correct == "python":
>         LETTERS = "python"

LETTERS always ends up == correct (and thus == word).  Why not just
use correct (or word)?

> chances = 0
> while chances < 5:
>     guess = input("Does the word have a: ")
>     for letter in correct:

You are looping over every letter in correct.  Why?
Don't you just want to loop over my 5 chances?

>         if letter.lower() in LETTERS:

Here's your other issue.  What is letter?  Based on your for loop,
it's one of the letters in correct.  What test do you actual intend to
do here?  Say it in english and the python code should be fairly
obvious.

>             print("\nYes")
>             chances += 1
>             guess = input("\nDoes the word have a: ")
>         else:
>             print("\nNo")
>             chances += 1
>             guess = input("\nDoes the word have a: ")

Just as a tip you've got a lot of repetition here.  If you move the
"chances" line and "guess" line outside of the if/else, your program
will be shorter and cleaner.

>     print("\nYour chances are up!")

You want this to occur when your chances are up, right?  So why is it
_inside_ the while loop?

Your basic approach is fine, you just have some extra stuff going on
unnecessarily and I'm guessing it's confused you as to your logic.
Give it another shot and let us know how it goes.

-- 
Brett Ritter / SwiftOne
swiftone at swiftone.org


More information about the Tutor mailing list