Home > Enterprise >  Game repeating output several times
Game repeating output several times

Time:11-11

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.

CodePudding user response:

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
  • Related