Python Forum
Printing During a Loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Printing During a Loop
#1
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!

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

.py   code.py (Size: 790 bytes / Downloads: 242)
Reply


Messages In This Thread
Printing During a Loop - by apeltes - Oct-19-2021, 04:02 AM
RE: Printing During a Loop - by DPaul - Oct-19-2021, 05:44 AM
RE: Printing During a Loop - by apeltes - Oct-19-2021, 07:00 AM
RE: Printing During a Loop - by buran - Oct-19-2021, 07:39 AM
RE: Printing During a Loop - by apeltes - Oct-19-2021, 04:15 PM
RE: Printing During a Loop - by buran - Oct-19-2021, 05:44 PM
RE: Printing During a Loop - by apeltes - Oct-19-2021, 11:37 PM
RE: Printing During a Loop - by ibreeden - Oct-20-2021, 07:30 AM
RE: Printing During a Loop - by buran - Oct-20-2021, 10:10 AM
RE: Printing During a Loop - by apeltes - Oct-20-2021, 02:50 PM
RE: Printing During a Loop - by buran - Oct-20-2021, 02:56 PM
RE: Printing During a Loop - by apeltes - Oct-20-2021, 03:19 PM
RE: Printing During a Loop - by buran - Oct-20-2021, 04:04 PM
RE: Printing During a Loop - by buran - Oct-20-2021, 04:08 PM
RE: Printing During a Loop - by apeltes - Oct-20-2021, 04:25 PM
RE: Printing During a Loop - by jefsummers - Oct-20-2021, 08:03 PM
RE: Printing During a Loop - by apeltes - Oct-21-2021, 12:19 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with for-loop printing. I want it to print only once. blacklight 2 6,952 Jun-26-2020, 02:23 AM
Last Post: pyzyx3qwerty
  My Loop for printing text file goes to infinity Nomex 7 4,224 Feb-10-2020, 03:13 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020