So this is my original code.
import random
hints = {"python" : "A program",
"jumble" : "The act of mixing up",
"easy" : "Facil is to Spanish as ____ is to English",
"difficult" : "Not eaay, but ____",
"answer" : "To ____ a question.",
"xylophone" : "An instrument. Also known as bells."}
words = ("python", "jumble", "easy", "difficult", "answer", "xylophone")
hint = 'hint'
word = random.choice(words)
word1 = word
correct = word
count = 1
jumble = ''
while word:
position = random.randrange(len(word))
jumble += word[position]
word = word[:position] + word[(position + 1):]
#.center(80) just centers the text on the python screen. Just to make the game
#look pretty.
print ">>>Welcome to Word Jumble<<<".center(80)
print "Unscramble the letters to make a word.".center(80)
print "(Press the enter key at the prompt to quit.)".center(80)
print "The jumble is:", jumble
print"\n\nFor a hint type in 'hint'."
while count >= 1:
guess = raw_input("Your guess:\n>>>")
guess = guess.lower()
if guess == hint:
if word1 == 'python':
print hints["python"]
elif word1 == 'jumble':
print hints["jumble"]
elif word1 == 'easy':
print hints["easy"]
elif word1 == 'difficult':
print hints["difficult"]
elif word1 == 'answer':
print hints["answer"]
elif word1 == 'xylophone':
print hints["xylophone"]
continue
while (guess != correct) and (guess != "") and (guess != 'hint'):
print "Sorry, that's not it."
guess = raw_input("Your guess:\n>>>")
guess = guess.lower()
if guess == correct:
print "That's it! You guessed it!\n"
break
print "Thanks for playing!"
raw_input("\n\nPress the enter key to exit.")
I am supposed to change these things:
>>instructions(), which displays instructions to the player. It takes no arguments and returns no values.
>>random_word(), which randomly selects the word to be guessed. It takes no arguments and returns the word to be guessed.
>>jumble(), which creates a jumble of the word to be guessed. It takes a word and returns a jumbled version of that word.
>>play(), which gets the player's guess until the guess is correct or the player quits. It takes the word to be guessed and a jumbled version of that word. It returns no values.
>>main(), whihc calls all of the other functions. It takes no values and returns no values. It should look something like this:
def main():
instructions()
the_word = random_word()
the_jumble == jumble(word)
play(the_word, the_jumble)
And this is what I have so far for using functions.
import random
words = ("python", "jumble", "easy", "difficult", "answer", "xylophone")
hints = {"python" : "A program",
"jumble" : "The act of mixing up",
"easy" : "Facil is to Spanish as ____ is to English",
"difficult" : "Not eaay, but ____",
"answer" : "To ____ a question.",
"xylophone" : "An instrument. Also known as bells."}
hint = 'hint'
jumble = ''
count = 1
def instruction():
print ">>>Welcome to Word Jumble<<<".center(80)
print "Unscramble the letters to make a word.".center(80)
print "(Press the enter key at the prompt to quit.)".center(80)
print"\n\nFor a hint type in 'hint'."
def random_word(words):
word = random.choice(words)
word1 = word
correct = word
return word
def jumble(word):
while word:
position = random.randrange(len(word))
jumble += word[position]
word = word[:position] + word[(position + 1):]
print "The jumble is:"
return jumble
def play():
while count >= 1:
guess = raw_input("Your guess:\n>>>")
guess = guess.lower()
if guess == hint:
if word1 == 'python':
print hints["python"]
elif word1 == 'jumble':
print hints["jumble"]
elif word1 == 'easy':
print hints["easy"]
elif word1 == 'difficult':
print hints["difficult"]
elif word1 == 'answer':
print hints["answer"]
elif word1 == 'xylophone':
print hints["xylophone"]
continue
while (guess != correct) and (guess != "") and (guess != 'hint'):
print "Sorry, that's not it."
guess = raw_input("Your guess:\n>>>")
guess = guess.lower()
if guess == correct:
print "That's it! You guessed it!\n"
break
def main():
instructions()
the_word = random_word()
the_jumble == jumble(word)
play(the_word, the_jumble)
print "Thanks for playing!"
raw_input("\n\nPress the enter key to exit.")
The problem I've encountered so far is in random_word(words) and jumble(word). It gives me this error:
Traceback (most recent call last):
File "<pyshell#50>", line 1, in <module>
jumble(word)
NameError: name 'word' is not defined
and I'm not quite sure what to do to define word. Should I leave it as a global variable? I feel like that would ruin the effect of having random_word(words).