首页 > 解决方案 > 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')

标签: pythonfunction

解决方案


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).


推荐阅读