[Tutor] Python Beginners
Dave Angel
davea at ieee.org
Wed Jun 8 13:00:23 CEST 2011
On 01/-10/-28163 02:59 PM, Vincent Balmori wrote:
> Hello. Right now I am learning the python language through Python Programming
> for the Absolute Beginner 3rd Edition. I am having trouble with one question in
> Ch. 4 #3, which says "Improve 'WordJumble so that each word is paired with a
> hint. The player should be able to see the hint if he or she is stuck. Add a
> scoring system that rewards players who solve a jumble without asking for the
> hint'".
>
> Right now I am having trouble with giving the 'hint' variable a value despite
> the conditions. Everything else is working fine.
>
Don't try to use color to give us any information, since this is a text
newsgroup. I wouldn't know you had tried if Valas hadn't mentioned yellow.
> # Word Jumble
> #
> # The computer picks a random word and then "jumbles" it
> # The player has to guess the original word
>
> import random
>
> # create a sequence of words to choose from
> 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
>
Since the words and hints are so closely tied, you should just make one
list/tuple with both of them, so that once you have chosen a word,
you've already got the hint.
WORDS = (("python","It's a snake"), ("jumble", Shake Those Words"), ...
then word, hint = random.choice(WORDS)
> # create a jumbled version of the word
> jumble =""
> while word:
> position = random.randrange(len(word))
> jumble += word[position]
> word = word[:position] + word[(position + 1):]
>
There's a random function that does this in one go. Look through the
module and see if you can find it.
> # hints for each word
>
> hint = None
> if word == "python":
> hint = ("It's a snake!!")
> elif word == "jumble":
> hint = ("Shake Those Words!")
> elif word == "easy":
> hint = ("Not Hard!")
> elif word == "difficult":
> hint = ("Not Easy!")
> elif word == "answer":
> hint = ("I ask a question you have an...")
> elif word == "xylophone":
> hint = ("Metal bars with two drum sticks")
>
Don't need this part any more.
DaveA
More information about the Tutor
mailing list