Terrax
Terrax

Reputation: 15

Need explanation on a while loop in Python

I am a beginner in Python and I am stuck on an exercise. In the book there is a game called Word Jumple. Here is what I need to do: 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 the hint.

And here is what I did:

# Word Jumble
#
# The computer 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")

# 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 a jumbled version of the word
jumble =""
hint = "False"

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 = input("\nYour guess: ")
while guess != correct and guess != "":
    if guess == "hint" and word == "python":
        hint = "True"
        print("It's a snake")
        guess = input("Your guess: ")
    elif guess == "hint" and word == "jumble":
        hint = "True"
        print("It's a game")
        guess = input("Your guess: ")
    elif guess == "hint" and word == "easy":
        hint = "True"
        print("It's type of difficulty")
        guess = input("Your guess: ")
    elif guess == "hint" and word == "difficulty":
        hint = "True"
        print("It's type of difficulty")
        guess = input("Your guess: ")
    elif guess == "hint" and word == "answer":
        hint = "True"
        print("It's the opposite of question")
        guess = input("Your guess: ")
    elif guess == "hint" and word == "xylophone":
        hint = "True"
        print("Don't know WTF is that")
        guess = input("Your guess: ")
    else:
        print("Sorry, that's not it.")
        guess = input("Your guess: ")

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

if hint == "False":
    print("Great! You did it without a hint")
else:
    print("Dat hint, man")

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

As a result I have this:

Welcome to Word Jumble!
Unscramble the letters to make a word.
(Press the enter key at the prompt to quit.)

The jumble is: jbelum

Your guess: hint
Sorry, that's not it.
Your guess: jumble
That's it! You guessed it!

Great! You did it without a hint
Thanks for playing.


Press the enter key to exit.

Why the while loop is missing all when the input is "hint" and goes directly to the else clause?

Thanks in advance for your time and help.

Upvotes: 0

Views: 724

Answers (3)

iblazevic
iblazevic

Reputation: 2733

musical_coder is right and solved your problem. Couple of more things you should change in your code to make it nicer:

Don't use strings for hint variable, use boolean:

instead:

hint = "False"
hint = "True"

use:

hint = False
hint = True

Also you can rewrite while loop a little bit to have

 guess = input("Your guess: ")

only once and not in every if.

Also, change result printing a bit cause this way it's gonna print hint information even if you input empty string and quit game.

So it could look something like this:

print("The jumble is:", jumble)
guess = "dummy"
while guess != correct and guess != "":
    guess = raw_input("Your guess: ")
    if guess == "hint" and correct == "python":
        hint = True
        print("It's a snake")
    elif guess == "hint" and correct == "jumble":
        hint = True
        print("It's a game")
    elif guess == "hint" and correct == "easy":
        hint = True
        print("It's type of difficulty")
    elif guess == "hint" and correct == "difficulty":
        hint = True
        print("It's type of difficulty")
    elif guess == "hint" and correct == "answer":
        hint = True
        print("It's the opposite of question")
    elif guess == "hint" and correct == "xylophone":
        hint = True
        print("Don't know WTF is that")
    else:
        print("Sorry, that's not it.")

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

    if not hint:
        print("Great! You did it without a hint")
    else:
        print("Dat hint, man")

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

Upvotes: 1

dansalmo
dansalmo

Reputation: 11686

Here is one way to make your program simpler:

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

Upvotes: 0

musical_coder
musical_coder

Reputation: 3896

The problem is that by the time you get to the while guess != correct and guess != "": loop, word has been completely jumbled. So in all the if and elif statements in that loop, you'll never have word equal to one of the six words in your list.

To fix this, substitute correct for word in those statements:

if guess == "hint" and correct == "python":
    hint = "True"
    print("It's a snake")
    guess = input("Your guess: ")
elif guess == "hint" and correct == "jumble":
    hint = "True"
    print("It's a game")
    guess = input("Your guess: ")
elif guess == "hint" and correct == "easy":
    hint = "True"
    print("It's type of difficulty")
    guess = input("Your guess: ")
elif guess == "hint" and correct == "difficulty":
    hint = "True"
    print("It's type of difficulty")
    guess = input("Your guess: ")
elif guess == "hint" and correct == "answer":
    hint = "True"
    print("It's the opposite of question")
    guess = input("Your guess: ")
elif guess == "hint" and correct == "xylophone":
    hint = "True"
    print("Don't know WTF is that")
    guess = input("Your guess: ")
else:
    print("Sorry, that's not it.")
    guess = input("Your guess: ")

Upvotes: 2

Related Questions