python - Python:如何创建多个函数重复工作并且每个函数工作的次数不同?
问题描述
我正在为水、眼睛、运动做一个“休息一下提醒”。问题是如果我使用 break 函数不会重复,如果我使用 continue 它们会无限工作。我希望每个功能工作不同的次数。我的程序的工作:用户将输入启动程序,它会在 28 分钟、30 分钟、48 分钟和 x、y、z 次后提醒他喝水、眼睛锻炼、体育锻炼
#import modules
import pygame #for playing reminder music
import time
# noinspection SpellCheckingInspection
# water reminder
def water():
totalbreaks2 = 2
breakcount2 = 0
while breakcount2 < totalbreaks2:
pygame.mixer.init()
pygame.mixer.music.load("water.mp3.mp3")
pygame.mixer.music.play(-1)
query = input('Did you drink water (type drank if yes)?')
if query == 'drank':
pygame.mixer.music.pause()
time.sleep(1)
breakcount2+=1
break
# noinspection SpellCheckingInspection
# eye exercise reminder
def eyes():
totalbreaks = 2
breakcount = 0
while breakcount < totalbreaks:
pygame.mixer.init()
pygame.mixer.music.load("eyes.mp3.mp3")
pygame.mixer.music.play(-1)
query = input('Did you do eyes exercise ?(type eydone if yes)')
if query == 'eydone':
pygame.mixer.music.pause()
time.sleep(1)
breakcount += 1
break
# noinspection SpellCheckingInspection
# physical exercise reminder
def exercise():
totalbreaks1 = 2
breakcount1 = 0
while breakcount1 < totalbreaks1:
pygame.mixer.init()
pygame.mixer.music.load("physical.mp3.mp3")
pygame.mixer.music.play(-1)
query = input('Did you do physical exercise (type exdone if yes) ?:')
if query == 'exdone':
pygame.mixer.music.pause()
time.sleep(1)
breakcount1+=1
z = input('Do you want to start the program? (y=yes , n=no):')
if z == 'y':
water()
eyes()
exercise()
elif z == 'n':
print('ok')
else:
print('type correct input')
解决方案
Instead of three loops, you could use one:
import time
import pygame
# settings
things = {
"water": {
"times": 2,
"every": 28,
},
"eye": {
"times": 2,
"every": 30,
},
"physical": {
"times": 2,
"every": 48,
},
}
# initial scheduling
schedule = []
for thing, data in things.items():
if data["times"] < 1:
continue
data["times"] -= 1
schedule.append((data["every"], thing))
schedule.sort()
# main loop; while we have things to do
while schedule:
delay, thing = schedule.pop(0)
print("Next thing:", thing, "in", delay, "minutes")
time.sleep(delay * 60)
pygame.mixer.init()
pygame.mixer.music.load(f"{thing}.mp3.mp3")
pygame.mixer.music.play(-1)
did = input(f"Did you do {thing}? Type yes if done> ") == "yes"
pygame.mixer.music.pause()
schedule = list((other_delay - delay, other) for (other_delay, other) in schedule)
if not did:
schedule.append((things[thing]["every"], thing))
elif things[thing]["times"] >= 1:
things[thing]["times"] -= 1
schedule.append((things[thing]["every"], thing))
schedule.sort()
The idea here is to keep a sorted list of currently scheduled reminders. When we slept enough for the earliest one, we reduce the delay in the other ones by the amount we slept, e.g.:
We schedule [(28, "water"), (30, "eye"), (48, "physical")]
. After waiting for 28 minutes and reminding the user of water, we subtract 28
from both remaining elements: [(2, "eye"), (18, "physical")]
. If we didn't do the task, or if there are instances of this task remaining (tracked through the "times"
key) we also reschedule the original task ("water"
) with the original delay (28 minutes).
推荐阅读
- c++ - 向下舍入到最接近的五的倍数 c++
- powershell - 根据从另一个脚本或 shell 调用来区分脚本中的代码
- android - 如何在 Android MusicKit 上创建 Apple MediaPlayerController
- babeljs - 从 Babel.transform 的结果中检索导入的路径
- c# - C# 控制台应用程序:写入 PowerShell 控制台而不发送到管道?
- ios - 创建自动为最终用户调用方法的 iOS 框架
- sql - 为什么我的 MariaDB SQL 查询将 INT 排序为 VARCHAR?
- javascript - 使用钩子插入 axios 结果
- regex - 当单词与数字相邻时使用单词边界
- javascript - 脚本 IF ELSE 仅处理 php 中的第一个数据