简体   繁体   中英

Python while loop to test two true/false elements

I am working on a word jumble game for my kids. I want them to be able to type 'hint' into the guess in order to get a hint. The first 'hint' should give the first and last letters of the word. The next time they type 'hint' should provide the first two and last two letters of the word.. etc.

I have it working for the first time they type 'hint' but then the while loop is broken and they can't guess incorrectly or type 'hint' again.

I know the issue is with this line:

while guess != correct and guess != 'hint':

I just can't seem to fix it so that the user can type hint more than once.

Here is my code:

# The computer picks a random word, and then "jumbles" it
# the player has to guess the original word

import random

replay = "y"
while replay == "y":

    print("\n"* 100)
    word = input("Choose a word for your opponent to de-code: ")
    print("\n"* 100)
    # provide a hint if the user wants one
    hint_count = 1

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

    # create a empty jumble word
    jumble = ""
    # while the chosen word has letters in it
    while word:
        position = random.randrange(len(word))
    #   add the random letter to the jumble word
        jumble += word[position]
    #   extract a random letter from the chosen word
        word = word[:position] + word[position +1:]

    # start the game
    print(
    """

                Welcome to the Word Jumble!

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

    guess = input("\nType 'hint' for help but lose 3 points. \
                  \nYOUR GUESS: ")

    while guess != correct and guess != 'hint':
        print("Sorry that's not it.")
        score -= 1
        guess = input("Your guess: ")

    if guess == 'hint':
        print("The word starts with '"+ correct[0:hint_count]+"' and ends with '"+correct[len(correct)-hint_count:len(correct)]+"'")
        score -= 3
        hint_count += 1
        guess = input("Your guess: ")

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


    print("Thanks for playing.")

    replay = input("\n\nWould you like to play again (y/n).")

After the first 'hint' the program should ask for a guess and then continue on to print("Thanks for playing.") since the conditional to check for hint is outside of the while loop. Put it into the while loop:

while guess != correct:
    if guess == 'hint':
        print("The word starts with '"+ correct[0:hint_count]+"' and ends with '"+correct[len(correct)-hint_count:len(correct)]+"'")
        score -= 3
        hint_count += 1
        guess = input("Your guess: ")

     else:
        print("Sorry that's not it.")
        score -= 1
        guess = input("Your guess: ")

The problem is that you check for guess == 'hint' outside the while loop. Try something like

while True:
    guess = input(...)
    if guess == correct:
        # correct
        break
    elif guess == 'hint':
        # show hint
    else:
        # not it, decrement score

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM