### Thread: The "Word Jumble" game

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
8
Rep Power
0

#### The "Word Jumble" game

Hey all, first time poster, not so long time lurker. Please be gentle...

For those familiar with the 3rd Edition of Python Programming for the Abs. Beginner by Dawson, Im modifying the word jumble game.

Using only code that was taught in the book up to the end of CH 4, (hence asking those familiar with the text) is there anything that could be done to simplify my modification to the word jumble game?

I've done everything the challenge asks for but was wondering if this was as clean as it could be done...

Thanks for any help.

Code:
```#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 random words to choose from
WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone")

#the hints
python = "THIS programming language."
jumble = "To shake things up."
easy = "The opposite of difficult."
difficult = "The opposite of easy."
answer = "Every question has an..."
xylophone = "A whacky instrument."

#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

#start the score off at zero
score = 0

#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 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("\nTo get a hint, type 'hint'")

if guess == "hint":
score += 1
if correct == 'python':
print(python)
elif correct == 'jumble':
print(jumble)
elif correct == 'easy':
print(easy)
elif correct == 'difficult':
print(difficult)
elif correct == 'xylophone':
print(xylophone)

while guess != correct and guess != "" and guess != "hint":
print("Sorry, that's not it.")

if guess == correct and score >= 1:
print("\nThat's it! You guessed it!")
print("\nThanks for playing, but there's no reward because you needed help.")

if guess == correct and score == 0:
print("\nThat's it! You guessed it!")
print("\nAND you needed NO help, here's a cookie: NOM! NOM! NOM! COOKIE!")
input("\nPress the enter key to exit.")```
2. The big tragedy of your program: you wrote the word list 4 times, and there would be no way to read the data into your program from a file.

I separated the data from the program, and made a few other minor changes.

Code:
```# this is a python3 code.

#Word Jumble
#The computer picks a random word and then "jumbles" it
#The player has to guess the original word

import random

# We will type the words and hints only once!
# This way we can easily expand the game.
hints = dict(
python = "THIS programming language.",
jumble = "To shake things up.",
easy = "The opposite of difficult.",
difficult = "The opposite of easy.",
answer = "Every question has an...",
xylophone = "A whacky instrument.",
)

WORDS = list(hints)                     # Yep, it's this easy

#pick one word randomly from the sequence
word = random.choice(WORDS)

#start the score off at zero
score = 0

#create a jumbled version of the word
L = list(word)                          # use random.shuffle for speed and
random.shuffle(L)                       # easier to understand than
jumble = ''.join(L)                     # your algorithm.

#start 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("\nTo get a hint, type 'hint'")

if guess == "hint":
score += 1
print(hints[word])                  # Yep, it's this easy.