Home > Mobile >  Creating a guessing game. Loop Question. I am a new programmer and first time poster. Any informatio
Creating a guessing game. Loop Question. I am a new programmer and first time poster. Any informatio

Time:04-09

I am brand new to Python world, so forgive me, this feels like a newbie type question. I am creating a small guessing game where the user will guess a word. The problem I am having is that once the user guesses the correct word, it gives them the correct output and then it will loop back to the beginning question "If you want to quit, please insert q for QUIT or y for PLAY."

This is what I want, but currently its simply outputting the answer from the previous game played. Any ideas how to loop back but have the system select a NEW random word? Here is my WHILE loop:

while loop:
print ("If you want to quit, please insert q for QUIT or y for PLAY")
play = input()
if play == 'q':
    loop = False
elif play == 'y':

    while guess != word:
        guess = input("Guess a word: ")

    if guess == word: 
        time.sleep(.9)
        print (f"Congrats, you won! the word was {word}")

CodePudding user response:

Supposing you already coded a logic to select the random word, just call the function inside the while loop.

while loop:
    word = select_random_word()

Also, after the user guesses each word, empty its value

guess == ''

CodePudding user response:

If you are storing the variables in an array, you should either increment the array by one at the end of the loop or use a random number with a seed using time like so. This would make the word order different every time you run the game.

import numpy as np
import time 
np.random.seed(int(time.time()))
np.random.randint(low = 1, high = 10, size = 10)

Then just change size to the amount of numbers you have in your array. This method doesn't eliminate the same word showing up multiple times though.

I could help more if you were to give how you select the word at the beginning of the code, and where your words are stored.

CodePudding user response:

you just need to initialize the word and guess when your game begins.

elif play == 'q':
     word = (Select your word from where you have your words)
     guess = None
     ...
  • Related