newb ?

mensanator at aol.com mensanator at aol.com
Fri Nov 18 03:08:24 CET 2005


Chad Everett wrote:
> Hey guys,
>
> I am back.  Trying to expand on  a program that was given in the book I am
> studying.
>
> No I am not a high school or college student.  Doing this on my own.  and
> having way to much trouble
>
> I am trying to add a hint section to a word jumble program.
>
> I get a traceback error that the word in the jumble is not defined.
> Can anyone help?

You have several errors.

> thanks,
>
> import random
>
> # create a sequence of words to choose from
> WORDS = ("python", "easy")
> # 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
> # create variable to use for hint if needed

You didn't create a variable here. Something like

hint = "hint"

And this is the core of your problems. You are confusing a
variable name with the value assigned to it.


> # create a jumbled version of the word
> jumble =""
> while word:
>     position = random.randrange(len(word))
>     jumble += word[position]
>     word = word[:position] + word[(position + 1):]
>
> # start the game
> print \
> """
>            Welcome to Word Jumble!
>
>    Unscramble the letters to make a word.
> (Press the enter key at the prompt to quit.)
> """
> print "The jumble is:", jumble
> print "If you need a hint type 'hint' and Hit enter."

Here you have specifically told the user that the hint word is "hint".

>
> guess = raw_input("\nYour guess: ")
> guess = guess.lower()
> while (guess != correct) and (guess != "")and (guess != hint): ##not sure
> about this either##

It will fail because you did not define the hint variable.
Note you actually don't need a hint variable (because the
hint word never changes), you could have said

... and (guess != "hint")

whereas (guess != correct) needs to compare against a variable
because correct does change.

>     print "Sorry, that's not it."
>     guess = raw_input("Your guess: ")
>     guess = guess.lower()
>
>
> ###I don"t lnow how to set up this part correct so that when they ask for
> the hint it will give it to them###
>
>
> while word == easy:

Same problem here, easy is a variable (which you haven't defined).
What you meant to say was

while word == "easy":


>     if guess == hint:
>         print "not hard but"
>         guess = raw_input("Your guess: ")
>         guess = guess.lower()
>
> while word == python:

Ditto. But this is going to get tedious when your word list gets large.
You should have your words and their hints stored in a dictionary:

hints = {'easy':'not hard but','python':'snake'}

then you need just a single code block that can print the hint
by using the correct word to look up the hint in the dictionary.

     if guess == hint:
         print hints[correct]
         guess = raw_input("Your guess: ")
         guess = guess.lower()
>
> if guess == correct:
>     print "That's it!  You guessed it!\n"
>
> print "Thanks for playing."
> 
> raw_input("\n\nPress the enter key to exit.")




More information about the Python-list mailing list