Home > Net >  Why does my print execute after the second loop even if I use print first?
Why does my print execute after the second loop even if I use print first?

Time:11-30

I'm a beginner of python, and I wanted to try to make a timer.

import time

sets=int(input("How many sets?: "))
seconds=int(input("How many seconds per set?: "))

for i in range(sets):
    print("set {0} of {1} started".format(i   1, sets))
    for j in range(seconds, 0, -1):
        print(j, end=" ")
print("Finished workout! Good Job!")

The problem is that the first print in the first loop is active after the j loop is ended, and I don't know why. Also my version of py is 3.11, I'm sorry if I misinterpreted the python-3.x tag.

I expected the output to be:

How many sets?: 3
How many seconds per set?: 2
set 1 of 3 started
2 1
set 2 of 3 started
2 1
set 3 of 3 started
2 1
Finished workout! Good Job!

But it's

How many sets?: 3
How many seconds per set?: 2
2 1 
set 1 of 3 started
2 1 
set 2 of 3 started
2 1 
set 3 of 3 started
Finished workout! Good Job!

Please help and thank you! :)

CodePudding user response:

it's not because of your loop , it's the way you print can you explain why you are using print(j, \end=" "\)?

CodePudding user response:

It's working for me on replit(Python 3.10)... Doesn't seem to be a code issue.

  • Related