1,105,546 Community Members

The Word Jumble Game

Member Avatar
Azul-P
Newbie Poster
7 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Greetings,

I'm working my way through Michael Dawson's Python Programming for the Absolute Beginner book. At the end of chapter 4: "for Loops, Strings and Tuples", there is a challenge that asks so "improve Word Jumble so that each word is paired with a hint. The player should be able to see the hint if he or she is stuck. Add a scoring system that rewards players who solve a jumble without asking for a hint."

Below is the code for the Word Jumble Game:

# Word Jumble
#
# The copmuter picks a random word and then "jumbles" it
# The player has to guess the original word

import random

# create a sequence of words to choose from
WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone")

# hints to the WORDS
HINTS = ("A programming language.", "Word _____.", "Antonym of difficult.",
         "Antonym of easy.", "Antonym of question.", "A musical instrument.")

# pick one randomly from the sequence
word = random.choice(WORDS)

# create a variable to use later to see if the guess is correct
correct = word

# 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 Jumble!

    Unscramble the letters to make a word.
(Press the enter key at the prompt to quit.)
"""
print "The jumble is:", jumble

guess = raw_input("\nYour guess: ")
guess = guess.lower()
while (guess != correct) and (guess != ""):
    print "Sorry that's not it."
    guess = raw_input("Your guess: ")
    guess = guess.lower


if guess == correct :
    print "That's it! You guessed right! \n"

print "Thanks for playing."

raw_input("\n\nPress the enter key to exit.")

I created a tuple with the hints in it. The indices of the hints are equivalent to the indices in words. Though I'm not really sure if that would work. Previous chapters covered: types, variables, branching and while loops. I'm also just skipping past this challenge and moving on to the next chapters. But its really bugging me that I did not solve this challenge.

Thanks,
Azul-P

Member Avatar
pyTony
pyMod
6,103 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

line 45 is missing () to call lower.

Member Avatar
griswolf
Veteran Poster
1,144 posts since Apr 2010
Reputation Points: 303 [?]
Q&As Helped to Solve: 264 [?]
Skill Endorsements: 3 [?]
 
0
 

I would have used a list of pairs, or maybe if there are a lot of options and looking up the hints is important, a dictionary, though that isn't the right thing here. Either of:

words_and_hints = [('python','snake or language'), ...]
# or (not really appropriate for this exercise)
words_to_hints = {'python':'sometimes eats prey or programmers for lunch',...}

Then at line 16: word,hint = random.choice(words_and_hints) When the user guesses at line 40, they can instead type a '?' (or whatever you prefer) which causes the program to display the hint and reduce the possible score for that round.

Member Avatar
Azul-P
Newbie Poster
7 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I tried your suggestion griswolf but now, even when I get the correct answer, it still outputs "Sorry, that's not it."

Member Avatar
pyTony
pyMod
6,103 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

You fixed line 45?

Member Avatar
Azul-P
Newbie Poster
7 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Yes, I have.

Member Avatar
griswolf
Veteran Poster
1,144 posts since Apr 2010
Reputation Points: 303 [?]
Q&As Helped to Solve: 264 [?]
Skill Endorsements: 3 [?]
 
1
 

Try adding lines "41.5" and "45.5" add print('You guessed "%s"'%guess) to see if there is something odd happening with the input.

By the way, to avoid the duplicate raw_input lines, you can use a loop like this, so you can call raw_input() just once at the top of the loop:

while True:
  guess = raw_input("Guess a word: ")
  lowguess = guess.lower()
  if lowguess == correct.lower():
    print("You guessed correctly!")
    break
  else:
    print('Sorry: Your guess of "%s" is not correct.'%guess)

(after the next round of changes, if you still have problems, it might be better to post the whole thing again, so line numbers stay in sync)

Member Avatar
woooee
Posting Maven
2,798 posts since Dec 2006
Reputation Points: 783 [?]
Q&As Helped to Solve: 836 [?]
Skill Endorsements: 12 [?]
 
0
 

Post the code as it is now. Note that you should now be using a tuple, so code has to be changed, like this example:

# pick one randomly from the sequence
word_tuple = random.choice(words_and_hints)
 
# create a variable to use later to see if the guess is correct
word = word_tuple[0]
correct = word
Member Avatar
Azul-P
Newbie Poster
7 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

When I added woooee's code first, it still gave an output of "Sorry, that's not it." But after I added griswolf's suggestion, it was able to give the correct outputs. I've also added the hints. Thanks for the help everyone! Now I understand how to grab just one element from a tuple inside a tuple.

# Word Jumble
#
# The copmuter picks a random word and then "jumbles" it
# The player has to guess the original word

import random

# create a sequence of words to choose from
wordsHints = (("python", "A programming language."),
         ("jumble", "Word _____."),
         ("easy", "Antonym of difficult."),
         ("difficult", "Antonym of easy."),
         ("answer", "Antonym of question."),
         ("xylophone", "A musical instrument."))

# pick one randomly from the sequence
wordTuple = random.choice(wordsHints)


# create a variable to use later to see if the guess is correct
word = wordTuple[0]
correct = word

# get the hint
hint = wordTuple[1]

# 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 Jumble!

    Unscramble the letters to make a word.
(Press the enter key at the prompt to quit.)
"""
print "The jumble is:", jumble

while True:
    guess = raw_input("Guess a word: ")
    lowguess = guess.lower()
    if lowguess == correct.lower():
        print("You guessed correctly!")
        break
    elif lowguess == "?":
        print hint
    else:
        print('Sorry: Your guess of "%s" is not correct.'%guess)


if guess == correct :
    print "That's it! You guessed right! \n"

print "Thanks for playing."

raw_input("\n\nPress the enter key to exit.")
Member Avatar
griswolf
Veteran Poster
1,144 posts since Apr 2010
Reputation Points: 303 [?]
Q&As Helped to Solve: 264 [?]
Skill Endorsements: 3 [?]
 
1
 

All that is left is to provide the scoring system... and maybe enjoy a little bit of tweaking. For instance here's how I would write a function to jumble the word letters:

def jumble(word):
  lword = list(word)
  random.shuffle(lword)
  return ''.join(lword)

The other thing I'd do is add an outer loop so the game keeps going until the player is done:

keep_asking = True
total_score = 0
loop_count = 0
while keep_asking:
  loop_count += 1
  # do one round of guessing, adding to the score
  keep_asking = raw_input("Guess again? ").lower().startswith('y')
print("Thanks for playing. Your score for %d rounds is %d"%(loop_count,total_score))
Member Avatar
pyTony
pyMod
6,103 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
1
 

Here is how I would write (have written today :) the jumble of word:

jumble = ''.join(random.sample(correct,len(correct)))

My correct is just word.lower(), and I do not call lower() for the target every time inside the loop.

I put the choosing of new word to function, which I call when points of correct answer is 0. After choosing I put the points to 10, for hint I cut that in half. I keep list of already chosen guesses and do not repeat those. Problem is also exhaustion of word list, which is not true in real game with reasonable number of guestions. Needed one extra quit condition from the game for that.

End of one game:

New word chosen!
The jumble 4: seya
Guess a word: easy

You guessed correctly! You got 10 points, your total is 35!
(Press the enter key at the prompt to quit the game.)

New word chosen!
The jumble 5: wsnaer
Guess a word: ?
Antonym of question.
The jumble 5: wsnaer
Guess a word: answer

You guessed correctly! You got 5 points, your total is 40!
(Press the enter key at the prompt to quit the game.)

New word chosen!
The jumble 6: ooylhxnep
Guess a word: xylophone

You guessed correctly! You got 10 points, your total is 50!
(Press the enter key at the prompt to quit the game.)

You answered all my guestions

Thanks for playing.
Your final result was 50.
Press the enter key to exit.
>>>
Question Answered as of 3 Years Ago by pyTony, griswolf and woooee
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: