Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I receive the updated time every time it is called in this function?

    import time
    seconds = time.time()
    local_time = time.ctime(seconds)
    print("Start Time:", local_time)
    def bot():
        x = 0
        while x < 16:
            time.sleep(1)
            print("Time:", local_time)
            x += 1

When I run this code it prints the initial time take before the function every time, I would like it to show the updated time every time local time is printed.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Using a variable will not re-evaluate it. You’ll need to calculate the time again each loop iteration

import time
    
def bot():
    for x in range(16):
        time.sleep(1)
        seconds = time.time()
        local_time = time.ctime(seconds)
        print("Time:", local_time)

bot()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading