Report this

What is the reason for this report?

python while loop

Posted on December 21, 2020

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



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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.

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

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.