Question

python while loop

Posted December 21, 2020 487 views
PythonDevelopment

im making a word guessing game and the guess will be the input and the word will be generated randomly.so if the guess=word,then i want the program to print that we got the number,but if the guess is not equal to word then how do u make the program to run from the point where the user inputs the guess?.pls help me

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi @anandu4115,

You can try something like the following

import random
#List of Words
wordsinrandom = ['Melon','Apple', 'Orange', 'Banana',]

#Take a random world from that list
randomWORD = random.choice(wordsinrandom)

#Make game active
game_active = True


#While game is active you'll always be in this loop
while game_active:
    userGuess = input('Please enter the Fruit in my mind: ')

    # If the user's guess is correct, stop the game
    if userGuess == randomWORD :
        game_active = False

    #Wrong guess, try again
    print('No luck! Please try again')

# END
print('Congratulations you guessed the fruit')

Regards,
KFSys