Python中的Pomodoro计时器

时间:2020-06-02 19:58:50

标签: python

我最近开始学习Python,并尝试编写番茄定时器程序。我已经编写了tasks()函数,并且可以正常工作,但是我不知道如何编写与break函数一起使用的tasks()函数。

我尝试过的事情:

  1. 我尝试将break函数与task()函数分开编写,并在task()函数内部调用break函数。
  2. 我已经在task()函数中编写了中断代码。
  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()

1 个答案:

答案 0 :(得分:0)

您可以将tasks()breaks()函数定义为foolows。还请注意,您尚未从用户那里获得任何输入来继续执行任务。您可以检查以下代码。我还定义了一个total_mins变量,用于跟踪完成任务的总时间。

import time

checkmark = 0
total_mins = 0

def tasks(task):
    global checkmark
    global total_mins
    mins=0
    print('Timer for ',task,' is 25 mins.')
    start=input('Press Enter to start the timer.')
    while mins <= 25:
        time.sleep(60)
        mins = mins + 1
        total_mins += 1
        print(mins, " minutes work completed.")
    print('End of Pomodoro')
    checkmark += 1
    print('Total check mark is ',checkmark)


def breaks():
    global checkmark
    mins = 0
    if checkmark <4:
        print('Take a short break.')
        while mins!=3:
            time.sleep(60)
            mins = mins + 1
            print(mins, " minutes break completed.")
        print('Break over')

    elif checkmark >=4:
        print('Take a long break.')
        while mins !=10:
            time.sleep(60)
            mins = mins + 1
            print(mins, " minutes break completed.")
        checkmark = 0
        print('Break over.')


def main():
    carry_on = 'y'
    task=input('Welcome to Pomodoro Timer\n What task do you want to work on? ')
    while carry_on=='y'or carry_on=='Y':
        tasks(task)
        breaks()
        carry_on = input("Do you want ot carry on?(y/n)")

    print("End of task ",task,". \nTotal time worked was minutes ", total_mins, " minutes.")

if __name__ == '__main__':
    main()