我最近开始学习Python,并尝试编写番茄定时器程序。我已经编写了tasks()
函数,并且可以正常工作,但是我不知道如何编写与break
函数一起使用的tasks()
函数。
我尝试过的事情:
如果有人能教我如何将中断功能与任务功能集成在一起,我将非常感激。
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()
答案 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()