猿问

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

我有一个while以厘秒为单位的循环计数并打印current_stepvar,总是在同一行。


我想跑步,例如,


x = True


while x is True:

    pass #printing timer to CLI here


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


月关宝盒
浏览 97回答 2
2回答

PIPIONE

你需要使用线程。import threadingx = Truedef thread_function():&nbsp; &nbsp; while x is True:&nbsp; &nbsp; &nbsp; &nbsp; pass #printing timer to CLI herethreading.Thread(target=thread_function).start()# Continue with the other steps you want to take# ...# This will terminate the timer loopx = FalsePython 线程文档:https ://docs.python.org/3/library/threading.html如果您想始终在同一行打印时间,则需要控制终端光标。

哆啦的时光机

我已经开始工作了。这是他们改编成我的脚本的答案:import threadingfrom os import systemtimer_thread = Nonedef timer(stawt, stahp, step, time_step):&nbsp; &nbsp; from time import sleep&nbsp; &nbsp; global run_timer&nbsp; &nbsp; stawt = int(stawt)&nbsp; &nbsp; stahp = int(stahp)&nbsp; &nbsp; if stahp < 1:&nbsp; &nbsp; &nbsp; &nbsp; print('Sorry, I limit min count to 1 second!\n')&nbsp; &nbsp; &nbsp; &nbsp; stahp = 1&nbsp; &nbsp; elif stahp > 1000:&nbsp; &nbsp; &nbsp; &nbsp; print('Sorry, I limit max count to 1000 seconds!\n')&nbsp; &nbsp; &nbsp; &nbsp; stahp = 1000&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; print()&nbsp; &nbsp; stahp = stahp * 100 + 1&nbsp; &nbsp; titerator = iter(range(stawt, stahp, step))&nbsp; &nbsp; def print_time():&nbsp; &nbsp; &nbsp; &nbsp; while run_timer is True:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current_step = str(next(titerator))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if int(current_step) < 99:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final_time = '0' + current_step[:0] + '.' + current_step[0:] + 's'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('\r' + final_time, end='')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif int(current_step) < 999:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final_time = current_step[:1] + '.' + current_step[1:] + 's'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('\r' + final_time, end='')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif int(current_step) < 9999:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final_time = current_step[:2] + '.' + current_step[2:] + 's'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('\r' + final_time, end='')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final_time = current_step[:3] + '.' + current_step[3:] + 's'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('\r' + final_time, end='')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sleep(time_step)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; except:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; seconds = int((int(current_step) / 100) % 60)&nbsp; &nbsp; &nbsp; &nbsp; minutes = int((int(current_step) / 100) // 60)&nbsp; &nbsp; &nbsp; &nbsp; if minutes < 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return ''&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final_time_human = str(minutes) + 'm ' + str(round(seconds)) + 's'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('\n' + final_time_human)&nbsp; &nbsp; print_time()def _init_timer():&nbsp; &nbsp; global run_timer; run_timer = True&nbsp; &nbsp; global timer_thread&nbsp; &nbsp; print('Enter max number of seconds to count: ', end='')&nbsp; &nbsp; count_to = float(input())&nbsp; &nbsp; timer_thread = threading.Thread(target=timer, args=(0, count_to, 1, 0.01))&nbsp; &nbsp; timer_thread.start()&nbsp; &nbsp; print('\rPress enter to stop the timer:')&nbsp; &nbsp; usr_input = input(); run_timer = Falsesystem('clear')_init_timer()timer_thread.join()print('\nGoodbye!')
随时随地看视频慕课网APP

相关分类

Python
我要回答