[Tutor] Python Beginners
Vincent Balmori
vincentbalmori at yahoo.com
Wed Jun 8 10:08:26 CEST 2011
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.
# 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
# create a jumbled version of the word
jumble =""
while word:
position = random.randrange(len(word))
jumble += word[position]
word = word[:position] + word[(position + 1):]
# 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")
# 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("\nFor a hint, type in 'yes'. If not, type in 'no'.")
helpcount = 0
help = input("Do you need a hint?: ")
if help == "yes":
print(hint)
helpcount += 1
guess = input("\nYour guess: ")
elif help == "no":
guess = input("\nYour guess: ")
while guess != correct and guess != "":
print("Sorry, that's not it.")
guess = input("Your guess: ")
if guess == correct:
print("That's it! You guessed it!")
if helpcount == 0:
print("And you didn't even need a hint! You're awesome!\n")
print("Thanks for playing.")
input("\n\nPress the enter key to exit.")
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110608/4d467d9d/attachment.html>
More information about the Tutor
mailing list