Use Python to make a simple fitness exercise program

content

1 Overview

2 Demand split

3 code implementation 


1 Overview

I blame myself for being a little straight, saying that she is fat, and to make up for it, I wrote this blog post to her as an apology. (I taught her to install python that day, so she can run it by herself, hahaha...)

 

I've been doing some exercise recently, and I saw a set of fat-burning videos on Douyin last night.
The content of the video tells how many actions are performed in each group.
Now there are all kinds of weight loss exercises on the Internet.
So the problem is, when you are exercising, you may be too tired to count how many exercises you have done.
Then you need to write a program to tell me how many exercises to do in each group. , what to do next.
Use Python to implement a simple exercise program today to achieve freedom of movement!
Realize simple sports broadcast function and simple motion count broadcast function.

2 Demand split

  • First of all, determine the structure of the data. The data structure of fitness exercise is how many actions are performed in each group of exercises. Then we can use a dictionary to specify this data structure.

  • Using the sports item as the key, how many actions need to be done as the value to store in a dictionary

  • Then we only need to write a loop to traverse this dictionary, and write a loop in the loop to count the number of sports items to complete the sports count.

3 code implementation 


#~~~~~~~~~~~~~·~导入相关库~~~~~~~~~~~~~~~~~~~·
'''pyttsx3是一个能让Python开口说话的库,pyttsx3通过初始化来获取语音引擎。
当我们第一次调用init操作的时候,会返回一个pyttsx3的engine对象,再次调用的时候,
如果存在engine对象实例,就会使用现有的,否则再重新创建一个。
pyttsx3属于第三方模块,如果你还未进行安装可以先安装这个库。'''
#~~~~~~~~~~欢迎关注公众号:电力系统与算法之美~~~~~~~~~·
#~~~~~~~~~·含有冰墩墩和雪容融代码~~~~~~~~~~~·
import pyttsx3  #这里使用pyttsx3这个库来进行语音播报。
import time

#~~~~~~~~~~~定义模型类~~~~~~~~~~~~~~~
class Sports():
    def __init__(self, sports, speed):
        sports = sports
        speed = speed

    @staticmethod
    def say(content):
        engine = pyttsx3.init()
        engine.say(content)
        engine.runAndWait()

    @staticmethod
    def doSport():
        for key, value in sports.items():

            index = list(sports).index(key) + 1
            # 如果是最后一个动作,播报:加油加油,最后一个动作啦~
            if index == len(sports):
                Sports.say('加油~加油~,最后一个动作啦!')
                print('加油~加油~,最后一个动作啦!')
            result = f'第{index}个动作:{key},一组{value}秒'
            print(f'\r{result}', end='\n')
            Sports.say(result)
            # 每做完一个动作,让用户休息3秒钟
            for i in [3, 2, 1, '开始~']:
                print(f'\r{i}', end='')
                Sports.say(i)
                time.sleep(speed)
            for i in range(1, value + 1):
                print(f'\r{i}', end='')
                Sports.say(i)
                time.sleep(speed)
        # 运动结束
        end = '运动结束,放松一下吧~'
        Sports.say(end)
'''有了运动模型类,我们就可以复用这个类啦,比如有n套健身运动,我们只需要实例化n个类就可以复用这个类了。'''

#~~~~~~~~~~~~~~~~运动方法:~~~~~~~~~~~~~~~~~`
@staticmethod
    def doSport():
        for key, value in sports.items():

            index = list(sports).index(key) + 1
            # 如果是最后一个动作,播报:加油加油,最后一个动作啦~
            if index == len(sports):
                Sports.say('加油~加油~,最后一个动作啦!')
                print('加油~加油~,最后一个动作啦!')
            result = f'第{index}个动作:{key},一组{value}秒'
            print(f'\r{result}', end='\n')
            Sports.say(result)
            # 每做完一个动作,让用户休息3秒钟
            for i in [3, 2, 1, '开始~']:
                print(f'\r{i}', end='')
                Sports.say(i)
                time.sleep(speed)
            for i in range(1, value + 1):
                print(f'\r{i}', end='')
                Sports.say(i)
                time.sleep(speed)
        # 运动结束
        end = '运动结束,放松一下吧~'
        Sports.say(end)

#~~~~~~~~~~`有了类模型,下面对类进行实例化,先传入4个动作来热热身吧~~~~~~~~~~~~~~
if __name__ == '__main__':
    sports = {'开合跳1': 30,
              '原地深蹲': 15,
              '高抬腿': 30,
              '胯下击掌': 15,
              }
    speed = 0.02  # 速度
    spor = Sports(sports, speed)
    spor.doSport()

Guess you like

Origin blog.csdn.net/weixin_46039719/article/details/123727128
Recommended