-3
if (ans1 == answerkey):
    print ("Correct. Good Job!!")
    right += 1
else:
    print ('Good try, here is the correct answers.')
    x = answerkey
    for i in range(1,initial_num2+1):
        if (i == x):
            print('[ >{}< ]'.format(i), end=' ')
        else:
            print('[{}]'.format(i), end=' ')

wrong += 1
time.sleep(15)

Can someone tell me why under the else statement, it would sleep for 15 seconds before running the "for i in range(1,initial_num2+1):" loop ? The only thing I can think of is the "end=' '" causing the issue because I am trying to have the loop print on the same line.

Aric
  • 3
  • 3

1 Answers1

1
  1. Python's print function is buffered, meaning it will sometimes wait until its internal buffer fills with data before printing. It typically waits until it sees a newline character before flushing the buffer to the console, so the buffering is especially apparent when you are writing very long lines of text (i.e., text that does not have any newline characters in it).
  2. The end argument tells the print function to not use newline characters to terminate the text, but to instead use whatever you give it.

Because you are suppressing newline characters, Python doesn't know to flush the text you gave to the print function until the program terminates after the sleep call. You can forcefully flush the print buffer by adding flush=True to the argument list you pass it.

See this answer and this one as well.

PaSTE
  • 4,050
  • 18
  • 26
  • thanks.. I change the code and have the loop output to a string then print once at the end.. – Aric Feb 09 '18 at 18:49