The main thing your program lacks is flexibility.  Adding new puzzles
requires chaining together a series of if..else statements and creating
variables for each hint.

Here's my quick version.  I store the puzzles and the hints in a two-tuple
sequence.  Following this method you could easily add additional hints for
each puzzle.

(I changed up the scoring system a bit too...just my spin on it)

import random

# create a series of puzzles to choose from
puzzles = (("python","It's the best programming language for the absolute
beginner ..."),
          ("jumble","It's what this program does to words to make it
difficult to guess them ..."),
          ("easy","It's not difficult ..."),
          ("difficult","It's not easy ..."),
          ("answer","It's not a question ..."),
          ("xylophone","It's a musical instrument you have to hit with 2
small sticks ..."))


# pick one word randomly from the sequence
which_puzzle = random.choice(range(len(puzzles)))
correct_word = puzzles[which_puzzle][0]
jumbled_word = list(correct_word)
hint = puzzles[which_puzzle][1]

random.shuffle(jumbled_word)
jumbled_word = ''.join(jumbled_word)

print \
"""
           Welcome to Word Jumple!

   Unscramble the letters to make a word.
   (Press the enter key at the prompt to quit.)
"""


score = 0
while 1:
   print "The jumble:", jumbled_word
   guess = raw_input("\nYour guess: ")
   guess = guess.lower()

   if guess == '':
           break

   if guess != correct_word:
       print "Sorry that's not it."
       hint_prompt = raw_input("Would you like a hint? Y/N: ")
       hint_prompt = hint_prompt.lower()
       if hint_prompt.startswith('y'):
           print hint+"\n"
           score -= 50

   if guess == correct_word:
       score += 200
       print "\nThat's it! You guessed it!\n"
       if score == 200:
           print "Because you never asked for a hint you get %d points.\n"
% (score)
       else:
           print "Since you asked for a hint or two you're score is %d
points.\n" % (score)
       break


print "\nThanks for playing."


On 4/17/07, Alexander Kapshuk <[EMAIL PROTECTED]> wrote:

 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.")



_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to