[Tutor] 'word jumble' game
Alexander Kapshuk
akap at isd.dp.ua
Tue Apr 17 16:31:09 CEST 2007
Hello Everyone,
This is Alexander Kapshuk writing here again ...
Could you please have a look at the code below and let me know of any
shortcuts that could be used there.
The code works fine as it is. I was just wandering if there was a
better, more compact and elegant way of writing the program.
Thanking you all in advance.
Alexander Kapshuk
# Word Jumble Game
#
# The computer picks a random word and then "jumbles" it.
# The player has to guess the original word.
#
# Should the player be stuck and require a hint, they will be prompted
for a hint.
# If the player answers 'yes', the appropriate hint will be displayed
and the player will be asked to guess again.
# If the player answers 'no', they will be asked to guess again and
awarded some points if they manage to guess the jumbled word without
ever asking for a hint.
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 hints for all the jumbled words
hint0 = "\nIt's the best programming language for the absolute beginner
...\n"
hint1 = "\nIt's what this program does to words to make it difficult to
guess them ...\n"
hint2 = "\nIt's not difficult ...\n"
hint3 = "\nIt's not easy ...\n"
hint4 = "\nIt's not a question ...\n"
hint5 = "\nIt's a musical instrument you have to hit with 2 small sticks
...\n"
# 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 Jumple!
Unscramble the letters to make a word.
(Press the enter key at the prompt to quit.)
"""
print "The jumble:", jumble
guess = raw_input("\nYour guess: ")
guess = guess.lower()
score = 0
while (guess != correct) and (guess != ""):
print "\nSorry, that's not it.\n"
hint_prompt = raw_input("Would you like a hint? Y/N: ")
hint_prompt = hint_prompt.lower()
if hint_prompt == "yes" and correct == WORDS[0]:
print hint0
elif hint_prompt == "yes" and correct == WORDS[1]:
print hint1
elif hint_prompt == "yes" and correct == WORDS[2]:
print hint2
elif hint_prompt == "yes" and correct == WORDS[3]:
print hint3
elif hint_prompt == "yes" and correct == WORDS[4]:
print hint4
elif hint_prompt == "yes" and correct == WORDS[5]:
print hint5
elif hint_prompt == "no":
score += 50
guess = raw_input("Your guess: ")
guess = guess.lower()
if guess == correct and hint_prompt == "no":
print "\nThat's it! You guessed it!\n"
print "Because you never asked for a hint you get", score,
"points.\n"
print "\nThanks for playing."
raw_input("\n\nPress the enter key to exit.")
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070417/9ef35d15/attachment.html
More information about the Tutor
mailing list