2

Im having a issue when programming. Im trying to use sleep() in the print(). The input is:

print(f'New year in 5{sleep(1)}.4{sleep(1)}.3{sleep(1)}.2{sleep(1)}.1{sleep(1)}. NEW YEAR!')

And the output is:

New year in 5None.4None.3None.2None.1None. NEW YEAR!

And the delay happens before print in the screen. Im using the last version of python. I'll be waiting for answers.

Jongware
  • 22,200
  • 8
  • 54
  • 100
  • 1
    Possible duplicate of [Display a countdown for the python sleep function](https://stackoverflow.com/questions/17220128/display-a-countdown-for-the-python-sleep-function) – Barney Jan 25 '18 at 15:48

3 Answers3

1

Try using time.sleep(1) (with module dot prefixed) instead of just sleep(1).

If you are just calling sleep(1) your script will search for a locally defined function instead of the function defined in module time.

hc_dev
  • 8,389
  • 1
  • 26
  • 38
  • It"s always helpful to add supporting links to your answer. Also some nice formatting may ease reading and highlight important spots. – hc_dev Jan 07 '20 at 03:08
0

Print can be called with a end attribute. The default value is a linebreak but you can choose to just put a space.

print('New year in 5', end=' ')

so the next thing you print will be on the same line.

This allow you to moove the sleep function outside of print.

print('New year in 5', end=' ')
sleep(1)
print('4', end=' ')
sleep(1)
print('3', end=' ')
# ...
cdrom
  • 591
  • 2
  • 16
0

Your code did not work as you expected because all objects passed to the print function are resolved and printed in one go.

Hence when you passed multiple sleep() they were all compiled and there was an initial wait and finally your message was printed..

And the None in your result was the return of time.sleep() function

Solution: Implement a countdown function Make sure that you print to the same line everytime, only change will be the time. This is achieved by using '\r' and end="" in python3 print function

But there is one small issue where te number of digits in you time reduces by one which can be handled by replacing the existing printed line with all white spaces

#!/usr/bin/python3
import time
def countdown(start):
    while start > 0:
        msg = "New year starts in: {} seconds".format(start)
        print(msg, '\r', end="")
        time.sleep(1)

        # below two lines is to replace the printed line with white spaces
        # this is required for the case when the number of digits in timer reduces by 1 i.e. from
        # 10 secs to 9 secs, if we dont do this there will be extra prints at the end of the printed line
        # as the number of chars in the newly printed line is less than the previous
        remove_msg = ' ' * len(msg)
        print( remove_msg, '\r', end="")        

        # decrement timer by 1 second
        start -= 1
    print("Happy New Year!!")
    return



if __name__ == '__main__':
    countdown(10)
satyakrish
  • 119
  • 5
  • what does the '\r' actually do? I have never see ntaht before. – Amir Dec 03 '19 at 10:30
  • 1
    @Amir `\r` is the [escaped string](https://docs.python.org/2.0/ref/strings.html) for [Carriage Return (CR)](https://en.wikipedia.org/wiki/Carriage_return). Here it may be used to print a new line. – hc_dev Jan 07 '20 at 03:05