📜  使用Python为程序员创建健康时钟

📅  最后修改于: 2022-05-13 01:54:57             🧑  作者: Mango

使用Python为程序员创建健康时钟

在本文中,我们将创建一个Python脚本来照顾一个人尤其是程序员的健康。它会在不同的指定时间间隔提醒用户喝水、做眼保健操和进行体育锻炼。它还会记录所有的锻炼时间、喝水情况以及锻炼次数和喝水量。在程序结束时,整个统计数据将与时间戳一起显示。

方法:

  • 我们将从pygame 模块导入混音器来播放我们的声音,从time 模块导入time来设置时间间隔,从time 模块导入 sleep 以在相应的时间间隔内冻结程序。
  • 我们将定义两个函数 - getdate()将在调用函数时返回当前日期和时间,以及musicloop()将播放给定文件。
  • 之后,我们将使用当前时间初始化水、眼保健操和体育锻炼变量。我们这样做是为了我们可以在相同的所需时间间隔播放声音。
  • 现在,我们将运行一个包含三个if条件的无限 while 循环(每个条件一个,即水、眼保健操和体育锻炼)。
  • 在每个if条件下,我们将做同样的事情,即调用 musicloop()函数来播放音乐,要求用户做相应的事情来停止音乐,并在文件中记录时间(完成练习)以便它最终可以显示。
  • 我们还将增加练习次数和饮水量,以便我们可以在程序结束时打印出来。
  • 最后,我们使用文件处理来显示所有上述数据。

下面是实现。

Python3
# import required modules
from pygame import mixer
from time import time
from time import sleep
  
  
def getdate():
    
    # To get the current date and time 
    # at time of entry
    import datetime
    return (str(datetime.datetime.now()))
  
  
def musicloop(stopper):
    mixer.init()
    mixer.music.load("music.mp3")
      
    # playing the music provided i.e. music.mp3
    mixer.music.play()  
    while True:
        x = input(
            "Please type STOP to stop the alarm or EXIT to stop the program : ")
          
        # music termination condition.
        if (x.upper() == stopper):
            print("\nGreat! Get back to work:)\n")
            mixer.music.stop()
            return True
            break
          
        # program termination condition.
        elif (x.upper() == "EXIT"):
            return False
  
  
# variables initialized with 0 for counting total 
# number of exercises and water drank in a day
total_water = 0
total_physical_exercises = 0
total_eye_exercises = 0
  
if __name__ == '__main__':
    print("\n\t\t\t\tHey Programmer! This is your Health-Alarm-Clock\n")
    time_phy = time()
    time_drink = time()
    time_eyes = time()
  
    eyes_delay = 10  
    drink_delay = 20  
    phy_delay = 35  
  
    while(True):
          
        # Drink water condition.
        if (time() - time_drink > drink_delay):
            print("Hey! Please drink some water (at least 200 ml).")
  
            # Checking the user input so that music 
            # can be stopped.
            if(musicloop("STOP")):
                pass
            else:
                break
  
            # reinitializing the variable
            time_drink = time()
              
            # incrementing the value
            total_water += 200
              
            # opening the file and putting the data 
            # into that file
            f = open("drank.txt", "at")
            f.write("[ " + getdate() + " ] \n")
            f.close()
  
        # Eye exercise condition.
        if (time() - time_eyes > eyes_delay):
  
            print("Hey! Please do an Eye Exercise.")
  
            if (musicloop("STOP")):
                pass
            else:
                break
  
            time_eyes = time()
            total_eye_exercises += 1
            f = open("eye.txt", "at")
            f.write("[ " + getdate() + " ] \n")
            f.close()
  
        # Eye exercise condition.
        if (time() - time_phy > phy_delay):
            print("Hey! Please do a Physical Exercise.")
  
            if (musicloop("STOP")):
                pass
            else:
                break
  
            time_phy = time()
            total_physical_exercises += 1
            f = open("phy_exer.txt", "at")
            f.write("[ " + getdate() + " ] \n")
            f.close()
  
    print()
    print(f"Total water taken today : {total_water}.")
      
    try:
        f = open("drank.txt", "rt")
        print("\nDetails :")
        print(f.read())
        f.close()
    except:
        print("Details not found!")
  
    print(f"Total eye exercise done today : {total_eye_exercises}.")
      
    try:
        f = open("eye.txt", "rt")
        print("\nDetails :")
        print(f.read())
        f.close()
    except:
        print("Details not found!")
  
    print(f"Total physical exercises done today : {total_physical_exercises}.")
      
    try:
        f = open("phy_exer.txt", "rt")
        print("\nDetails :")
        print(f.read())
        f.close()
    except:
        print("Details not found!")
  
    sleep(5)


输出:



视频演示: