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

Recommended Answers

All 4 Replies

Should you be using "the_word", since that is what is returned from random_word()?

This is what I ended up with. I don't think its %100 correct, but I'll take the couple of points off for missing out on variable such as the_word, the_jumble, and the_hint. It works, and, to me, that is all that matters.

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 easy, but ____", 
               "answer" : "To ____ a question.",
               "xylophone" : "An instrument. Also known as bells."}
word = random.choice(words)
hint = 'hint'
correct = word
word1 = word
count = 1



def instruction():
    print ">>>Welcome to Word Jumble<<<".center(80)
    print "Unscramble the letters to make a word.".center(80)
    print "Don't type anything and hit enter to exit.".center(80)
    print"\n\nFor a hint type in 'hint'."

def random_word():
    word
    return word


def jumble(word):
    jumble = ''
    while word:
        position = random.randrange(len(word))
        jumble += word[position]
        word = word[:position] + word[(position + 1):]
    print "The jumble is:",jumble

def play():
    while count >= 1:
        guess = raw_input("Your guess:\n>>>")
        guess = guess.lower()
        if guess == "":
            print "Good-bye!"
            break
        elif 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():
    instruction()
    random_word()
    jumble(word)
    play()

main()

print "Thanks for playing!"
raw_input("\n\nPress the enter key to exit.")

A suggestion:

if guess == "":
            print "Good-bye!"
            ##break
            return  ## exit play() function
        elif (guess == hint) and (word1 in hints):
            print hints[word1]

        """
            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'):
# replace with
        while guess not in [correct, "", 'hint']:
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.