简体   繁体   中英

How do you make two functions work side by side in python?

I am very new to python and am taking a course at my school, I was given the homework of making a clock that counts down from 1 or 2 hours and also shows the minutes seconds and hours the whole time. I started to do the code and defined 2 functions, seconds, and minutes. Seconds counts down from 60 seconds and minutes does the same thing except from 1 minute, I tried them seperatly and they worked, then I tried them together and I couldn't get them to work side by side. How can I make them do this, also, should I just be using a variable that counts down? Any help is appreciated.

from time import *
def seconds():
    while 1==1:
        time_s = 60
        while time_s != 0:
            print (time_s)
            sleep(1)
            time_s=time_s-1

            os.system( [ 'clear', 'cls' ][ os.name == 'nt' ] )

def minutes():
    while 1==1:
        time_m = 60
        while time_m!= 0:
            print (time_m)
            sleep(60)
            time_m = time_m-1`

Also, indents might be messed up.

As there are sixty seconds in a minute. You don't need to calculate them separately. Just calculate the total amount of seconds and divide by 60 to show the minutes, and modulo 60 to show the seconds.

I think your program needs lot of alteration regarding threading

this link should help u with that :-)

http://www.doughellmann.com/PyMOTW/threading/

Your Desired Complete Program

    import threading
    import logging
    import time

    time_m=60
    time_s=60
    time_h=24

    print ('Karthick\'s death Clock Begins')

    def seconds():
       while 1==1:
          global time_s,time_m,time_h
          time_s = 60
          while time_s != 0:
              print (time_h,':',time_m,':',time_s)
              time.sleep(1)
              time_s=time_s-1

              os.system( [ 'clear', 'cls' ][ os.name == 'nt' ] )

    def minutes():
        while 1==1:
            global time_m
            time_m = 60
            while time_m!= 0:
                time.sleep(60)
                time_m = time_m-1

    def hours():
        while 1==1:
            global time_h
            time_h = 24
            while time_h!= 0:
                time.sleep(360)
                time_h = time_h-1

m=threading.Thread(name='minutes',target=minutes)
s=threading.Thread(name='seconds',target=seconds)
h=threading.Thread(name='hours',target=hours)

m.start()
s.start()
h.start()

Enjoy Programming :-)!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM