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

line 45 is missing () to call lower.

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.

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

You fixed line 45?

Yes, I have.

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)

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

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

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

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