The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
The "Word Jumble" game
Discuss The "Word Jumble" game in the Python Programming forum on Dev Shed. The "Word Jumble" game Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 8th, 2012, 06:24 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 8
Time spent in forums: 4 h 58 m 58 sec
Reputation 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'")
guess = input("\nYour guess: ")
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 == 'answer':
print(answer)
elif correct == 'xylophone':
print(xylophone)
guess = input("Your guess: ")
while guess != correct and guess != "" and guess != "hint":
print("Sorry, that's not it.")
guess = input("Your guess: ")
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.")
|

October 8th, 2012, 07:23 PM
|
 |
Contributing User
|
|
|
|
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'")
guess = input("\nYour guess: ")
if guess == "hint":
score += 1
print(hints[word]) # Yep, it's this easy.
guess = input("Your guess: ")
while guess and (guess != word) and (guess != "hint"):
print("Sorry, that's not it.")
guess = input("Your guess: ")
if guess == word: # this was a repeated test, so I nested the subordinates
if score:
print("\nThat's it! You guessed it!")
print("\nThanks for playing, but there's no reward because you needed help.")
else:
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.")
__________________
[code] Code tags[/code] are essential for python code!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|