【问题标题】:How do you make two functions work side by side in python?你如何让两个函数在 python 中并排工作?
【发布时间】:2012-10-20 15:33:09
【问题描述】:

我对 python 非常陌生,并且正在我的学校学习一门课程,我的作业是制作一个从 1 或 2 小时倒计时的时钟,并且始终显示分秒和小时。我开始编写代码并定义了 2 个函数,秒和分钟。秒从 60 秒开始倒计时,而分钟从 1 分钟开始倒计时,我分别尝试它们并且它们起作用,然后我一起尝试它们并且我无法让它们并排工作。我怎样才能让他们这样做,我是否应该只使用一个倒计时的变量?任何帮助表示赞赏。

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`

另外,缩进可能会搞砸。

【问题讨论】:

  • 是的,他们这样做了,我只被允许使用一定数量的模块,比如时间线程等。我不允许使用我电脑的内部时钟,因为它太“简单”了,但感谢提示,我会记住以供将来参考。

标签: python python-3.x


【解决方案1】:

因为一分钟有六十秒。您不需要单独计算它们。只需计算总秒数并除以 60 以显示分钟,然后模 60 以显示秒。

【讨论】:

    【解决方案2】:

    您想要的完整计划

        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()
    

    享受编程:-)!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-01
      • 2023-03-06
      • 2011-11-09
      • 2014-10-30
      • 2016-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多