为了账号安全,请及时绑定邮箱和手机立即绑定

继续执行代码而不中断循环函数的输出

继续执行代码而不中断循环函数的输出

月关宝盒 2022-12-20 11:09:05
我有一个while以厘秒为单位的循环计数并打印current_stepvar,总是在同一行。我想跑步,例如,x = Truewhile x is True:    pass #printing timer to CLI hereprint('this is more code running while the timer is still running')input('Press enter to stop the timer')x = False#When x becomes False, I want the while loop to terminate我知道这一定涉及子流程或类似的东西,但我不知道要为学习解决这个问题指明自己的方向。这是供参考的功能:def timer(stawt, stahp, step, time_step):    from time import sleep    stawt = int(stawt)    stahp = int(stahp)    if stahp < 1:        stahp = 1    elif stahp > 1000:        stahp = 1000    stahp = stahp * 100 + 1    titerator = iter(range(stawt, stahp, step))    while True:        try:            current_step = str(next(titerator))            if int(current_step) < 99:                final_time = '0' + current_step[:0] + '.' + current_step[0:] + 's'                print('\r' + final_time, end='')            elif int(current_step) < 999:                final_time = current_step[:1] + '.' + current_step[1:] + 's'                print('\r' + final_time, end='')            elif int(current_step) < 9999:                final_time = current_step[:2] + '.' + current_step[2:] + 's'                print('\r' + final_time, end='')            else:                final_time = current_step[:3] + '.' + current_step[3:] + 's'                print('\r' + final_time, end='')            sleep(time_step)        except:            print(); break    seconds = int((int(current_step) / 100) % 60)    minutes = int((int(current_step) / 100) // 60)    if minutes < 1:        return ''    else:        final_time_human = str(minutes) + 'm ' + str(round(seconds)) + 's'        print(final_time_human + '\n')def MAIN():    count_to = float(input('Enter max number of seconds to count:\n'))    print()    timer(0, count_to, 1, 0.01)MAIN()
查看完整描述

2 回答

?
PIPIONE

TA贡献1829条经验 获得超9个赞

你需要使用线程。


import threading


x = True


def thread_function():

    while x is True:

        pass #printing timer to CLI here


threading.Thread(target=thread_function).start()


# Continue with the other steps you want to take

# ...



# This will terminate the timer loop

x = False

Python 线程文档:https ://docs.python.org/3/library/threading.html


如果您想始终在同一行打印时间,则需要控制终端光标。


查看完整回答
反对 回复 2022-12-20
?
哆啦的时光机

TA贡献1779条经验 获得超6个赞

我已经开始工作了。这是他们改编成我的脚本的答案:


import threading

from os import system


timer_thread = None


def timer(stawt, stahp, step, time_step):

    from time import sleep

    global run_timer


    stawt = int(stawt)

    stahp = int(stahp)


    if stahp < 1:

        print('Sorry, I limit min count to 1 second!\n')

        stahp = 1

    elif stahp > 1000:

        print('Sorry, I limit max count to 1000 seconds!\n')

        stahp = 1000

    else:

        print()


    stahp = stahp * 100 + 1

    titerator = iter(range(stawt, stahp, step))


    def print_time():

        while run_timer is True:

            try:

                current_step = str(next(titerator))

                if int(current_step) < 99:

                    final_time = '0' + current_step[:0] + '.' + current_step[0:] + 's'

                    print('\r' + final_time, end='')

                elif int(current_step) < 999:

                    final_time = current_step[:1] + '.' + current_step[1:] + 's'

                    print('\r' + final_time, end='')

                elif int(current_step) < 9999:

                    final_time = current_step[:2] + '.' + current_step[2:] + 's'

                    print('\r' + final_time, end='')

                else:

                    final_time = current_step[:3] + '.' + current_step[3:] + 's'

                    print('\r' + final_time, end='')


                sleep(time_step)

            except:

                break


        seconds = int((int(current_step) / 100) % 60)

        minutes = int((int(current_step) / 100) // 60)


        if minutes < 1:

            return ''

        else:

            final_time_human = str(minutes) + 'm ' + str(round(seconds)) + 's'

            print('\n' + final_time_human)


    print_time()


def _init_timer():

    global run_timer; run_timer = True

    global timer_thread


    print('Enter max number of seconds to count: ', end='')

    count_to = float(input())


    timer_thread = threading.Thread(target=timer, args=(0, count_to, 1, 0.01))

    timer_thread.start()


    print('\rPress enter to stop the timer:')

    usr_input = input(); run_timer = False


system('clear')

_init_timer()


timer_thread.join()

print('\nGoodbye!')


查看完整回答
反对 回复 2022-12-20
  • 2 回答
  • 0 关注
  • 94 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信