Game repeating output several times

Question:

I’m making a small scrambled word game and am trying to implement a ‘rescramble’ feature. So far, if the user chooses to rescramble the word, I call the game function and place in the same input that was originally provided. The second iteration of the function will scramble the word into a unique form and the game continues. Here’s the code and sample output:

def game(n,d):
    a = True
    scrambled = shuffler(n)
    guess = input(f"{scrambled}  're' to rescramble:  ")
    if guess == 're':
        n = n
        d = d
        game(n,d)
    elif guess == n:
        print("Correct")
    else:
        print("Incorrect")
        a = False
    print(n,":",d)
    return a

stigeev  're' to rescramble:  re
gveetsi  're' to rescramble:  re
eeisgvt  're' to rescramble:  vestige
Correct
vestige : a trace of something that is disappearing or no longer exists
vestige : a trace of something that is disappearing or no longer exists
vestige : a trace of something that is disappearing or no longer exists
Score [1/1]

For some reason the Incorrect/Correct prints once but the definition prints as many times as the word is rescrambled. I assume that my problem stems from calling the function over and over again, but I am unsure of how to circumvent this.

Asked By: Beta Chad

||

Answers:

This does your looping correctly.

def game(n,d):
    while True:
        scrambled = shuffler(n)
        guess = input(f"{scrambled}  're' to rescramble:  ")
        if guess != 're':
            break
    if guess == n:
        print("Correct")
    else:
        print("Incorrect")
    print(n,":",d)
    return guess == n
Answered By: Tim Roberts
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.