猿问

Python 中的番茄钟计时器

我最近开始学习Python并尝试编写一个番茄钟程序。我已经编写了tasks()函数并且它工作正常但我不知道如何编写与函数break一起工作的tasks()函数。

我尝试过的事情:

  1. 我尝试编写与 tasks() 函数分开的 break 函数,并在 tasks() 函数中调用 break 函数。

  2. 我已经在 tasks() 函数中编写了中断代码。

  3. 在其中定义主要功能和书面中断功能,但没有任何效果。我试过用谷歌搜索但找不到答案。

如果有人能教我如何将中断功能与任务功能集成在一起,我将非常感激。

import time


checkmark=0

def tasks():

    global checkmark

    carry_on='y'

    while carry_on=='y'or carry_on=='Y':

        min=0

        task=input('What task do you want to work on?')

        print('timer for ',task,' is 25 mins')

        start=input('Press Enter to start the timer.')

        while min!=1:

            time.sleep(60)

            min=min+1

        print('End of task')

        checkmark=checkmark+1

        print('Total check mark is ',checkmark)


def main():

    tasks()

    mins=0

    if checkmark <4:

        print('take a short break')

        while mins!=3:

            time.sleep(60)

            mins=mins+1

            print('break over')

    elif checkmark >=4:

        print('Take a long break')

        while mins !=10:

            time.sleep(60)

            mins=mins+1

        print('Break over')

    else:

        tasks()


if __name__ == '__main__':

    main()


胡说叔叔
浏览 92回答 1
1回答

翻过高山走不出你

您可以将tasks()和breaks()函数定义为傻瓜。另请注意,您没有从用户那里获取任何输入来决定是否继续执行任务。您可以检查下面的代码。我还定义了一个total_mins变量,用于跟踪完成任务的总时间。import timecheckmark = 0total_mins = 0def tasks(task):&nbsp; &nbsp; global checkmark&nbsp; &nbsp; global total_mins&nbsp; &nbsp; mins=0&nbsp; &nbsp; print('Timer for ',task,' is 25 mins.')&nbsp; &nbsp; start=input('Press Enter to start the timer.')&nbsp; &nbsp; while mins <= 25:&nbsp; &nbsp; &nbsp; &nbsp; time.sleep(60)&nbsp; &nbsp; &nbsp; &nbsp; mins = mins + 1&nbsp; &nbsp; &nbsp; &nbsp; total_mins += 1&nbsp; &nbsp; &nbsp; &nbsp; print(mins, " minutes work completed.")&nbsp; &nbsp; print('End of Pomodoro')&nbsp; &nbsp; checkmark += 1&nbsp; &nbsp; print('Total check mark is ',checkmark)def breaks():&nbsp; &nbsp; global checkmark&nbsp; &nbsp; mins = 0&nbsp; &nbsp; if checkmark <4:&nbsp; &nbsp; &nbsp; &nbsp; print('Take a short break.')&nbsp; &nbsp; &nbsp; &nbsp; while mins!=3:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time.sleep(60)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mins = mins + 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(mins, " minutes break completed.")&nbsp; &nbsp; &nbsp; &nbsp; print('Break over')&nbsp; &nbsp; elif checkmark >=4:&nbsp; &nbsp; &nbsp; &nbsp; print('Take a long break.')&nbsp; &nbsp; &nbsp; &nbsp; while mins !=10:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time.sleep(60)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mins = mins + 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(mins, " minutes break completed.")&nbsp; &nbsp; &nbsp; &nbsp; checkmark = 0&nbsp; &nbsp; &nbsp; &nbsp; print('Break over.')def main():&nbsp; &nbsp; carry_on = 'y'&nbsp; &nbsp; task=input('Welcome to Pomodoro Timer\n What task do you want to work on? ')&nbsp; &nbsp; while carry_on=='y'or carry_on=='Y':&nbsp; &nbsp; &nbsp; &nbsp; tasks(task)&nbsp; &nbsp; &nbsp; &nbsp; breaks()&nbsp; &nbsp; &nbsp; &nbsp; carry_on = input("Do you want ot carry on?(y/n)")&nbsp; &nbsp; print("End of task ",task,". \nTotal time worked was minutes ", total_mins, " minutes.")if __name__ == '__main__':&nbsp; &nbsp; main()
随时随地看视频慕课网APP

相关分类

Python
我要回答