Hello! I am doing a project for a beginning coding course.
It's a simple guessing game. The user guesses a secret number.
The code has a while loop. It is supposed to print a string during each iteration of the loop, telling the user if the guess is too high or too low.
But nothing at all prints until the loop is broken (a list of responses is printed all at once when the game is over).
Why is this? Why does all the printing get "saved up" like this? I've searched the internet for answers and I've had no luck.
Thank you!
It's a simple guessing game. The user guesses a secret number.
The code has a while loop. It is supposed to print a string during each iteration of the loop, telling the user if the guess is too high or too low.
But nothing at all prints until the loop is broken (a list of responses is printed all at once when the game is over).
Why is this? Why does all the printing get "saved up" like this? I've searched the internet for answers and I've had no luck.
Thank you!
def main(): # Initialize variables numGuesses = 0 userGuess = -1 secretNum = 5 name = input("Hello! What is your name?") while(userGuess != secretNum): userGuess = int(input("Guess a number between 1 and 20: ")) numGuesses = numGuesses + 1 if (userGuess < secretNum): print("You guessed " + str(userGuess) + ". Too low.") if (userGuess > secretNum): print("You guessed " + str(userGuess) + ". Too high.") print("Hello, " + (name) + ".") print(“You guessed the secret number.”) print("It is true that the secret number was “ + str(secretNum) + ".”) print(“It took you “ + str(numGuesses) + " tries.”) main()
Attached Files