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: 238)
Reply
#2
It is very difficult to give advice,
if the code is not properly indented , using python tags.

Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
Of course. I understand. I took some time to look at posting requirements and I think I figured out how to post my code in a way that preserves indentation. I hope this is better! Thank you for the feedback!

(Oct-19-2021, 05:44 AM)DPaul Wrote: It is very difficult to give advice,
if the code is not properly indented , using python tags.

Paul
Reply
#4
After fixing the bunch of SyntaxErrors due to wrong quotes you use here and there your code works for me

Output:
Hello! What is your name?John Doe Guess a number between 1 and 20: 3 You guessed 3. Too low. Guess a number between 1 and 20: 5 Hello, John Doe. You guessed the secret number. It is true that the secret number was 5. It took you 2 tries.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Interesting! I don't see any problems with the quotes... can you give me an example of where you had to make a fix?
Thank you!

(Oct-19-2021, 07:39 AM)buran Wrote: After fixing the bunch of SyntaxErrors due to wrong quotes you use here and there your code works for me

Output:
Hello! What is your name?John Doe Guess a number between 1 and 20: 3 You guessed 3. Too low. Guess a number between 1 and 20: 5 Hello, John Doe. You guessed the secret number. It is true that the secret number was 5. It took you 2 tries.
Reply
#6
line 25 “You guessed the secret number.” - both opening and closing one
line 26 ".” - the closing one
line 27 - “It took you “ both opening and closing one

the correct ones are "

if you look at your code in the first post - the incorrect ones are black, while the correct ones are blue
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Wow. I didn't know there were different kinds of quotation marks. Thanks for showing me that!

But I fixed the code so that all the quotation marks are the right kind, and it doesn't change how the program runs. It still prints everything all at once when the secret number is guessed.

By the way, I'm running my code with "Python Idle". That's what they have us using for the class. I don't know if that makes a difference.

Here's the code with the quotes fixed.

# Heading (WC,9/13/21,) feel free to use multiple lines

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()
Reply
#8
(Oct-19-2021, 11:37 PM)apeltes Wrote: it doesn't change how the program runs. It still prints everything all at once when the secret number is guessed.
What do you mean? Can you show us your output? This is the output I get. Seems OK.
Output:
Hello! What is your name?ibreeden Guess a number between 1 and 20: 5 Hello, ibreeden. You guessed the secret number. It is true that the secret number was 5. It took you 1 tries.
Reply
#9
It's obvious that you run something [completely] different from what you post - i.e. with the incorrect quotes it will not run at all and you haven't noticed them, the different output you claim, etc.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
I've double-checked, and the type of quotes make no difference when I'm using Python Idle. My code was running before I changed that. That's why I didn't notice the difference. I used Word to work on the code at one point because Python Idle kept timing out, and that's probably why some of the quotation marks are different. I copied the code back into Python Idle when it was done and it ran.

When I run the code, I get a pop-up for each input prompt. Each time I make an incorrect guess there is no output. Just another pop-up asking for a guess.

Then, when I guess correctly (5), the output lists all of the print statements at one time. Telling me about all of my incorrect guesses and my correct guess.

I asked the teacher for help, and she says that's what is supposed to happen for this assignment.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with for-loop printing. I want it to print only once. blacklight 2 6,860 Jun-26-2020, 02:23 AM
Last Post: pyzyx3qwerty
  My Loop for printing text file goes to infinity Nomex 7 4,148 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