Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 8th, 2012, 06:24 PM
TheLetterZero TheLetterZero is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 8 TheLetterZero User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.") 

Reply With Quote
  #2  
Old October 8th, 2012, 07:23 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,350 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 38 m 45 sec
Reputation Power: 383
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > The "Word Jumble" game

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap