计算糖尿病患者的胰岛素水平[关闭]

问题描述 投票:0回答:1

我正在寻找我所上课的代码复习。我试图了解OOP,所以我一直在建模简单的问题

该代码模拟了一种情况,即糖尿病患者的血糖水平低或高,而我的班级正像胰岛素泵那样。

我总体上试图更好地理解类和OOP,所以我一直在尝试对简单情况进行建模,例如编写程序来模拟饥饿的人。我最近写了一小堂课,我想知道这是否是OOP的用例。我想知道是否有人可以看一下我的代码并给我评论。

# A program used to model or help someone with diabetes.
# I created this program to practice classes
import random

class Person():
    """A class to represent and help a person with diabetes"""
    ideal_blood_suger = 100

    def __init__(self,name,age,sex,type_of_diabetes):

        self.name = name
        self.age = age
        self.sex = sex
        self.type_of_diabetes = type_of_diabetes

    def describe_self(self):
        """A function to describe the person"""
        return f'My Name is {self.name} and I am {self.age} years old. I am a {self.sex} and have ' \
               f'{self.type_of_diabetes}'
    def low_asking_for_help(self):
        '''A Function used to call for help when the user has a low'''
        print(f'Hello {self.name} The alarm is being activated')
        print(f'We just called the 911 and informed them that you are having a low. Help will be arriving '
              f'at your location soon {self.name}')

    def high_asking_for_help(self):
        '''A function used to call for help when the user has a high'''
        print(f'Hello {self.name} The alarm is being activated')
        print(f'We just called the 911 and informed them that you are having a high. Help will be arriving '
              f'at your location soon {self.name}')

    def blood_sugar(self):
        '''A function used to generate blood sugar levels'''
        blood_sugar_levels = []
        for i in range(15):
            i = random.randint(60,200)
            blood_sugar_levels.append(i)
        return blood_sugar_levels

    def calculate_insulin(self):
        '''A function used to calculate the insulin levels and determine if something is to be done'''
        blood_sugar_levels = self.blood_sugar()
        for num in blood_sugar_levels:
            if num> self.ideal_blood_suger + 30:
                print(f"Your Blood Sugar level is to high {self.name}. Your Blood Sugar Level is currently at {num}. "
                      f"You have to administer some insulin to yourself.")
                while True:
                    try:
                        administered = input(f"{self.name}, Did You administer the insulin to yourself enter y for yes "
                                             f"or n for no. If you need help enter h: ")
                        if administered == 'y':
                            break
                        elif administered == 'n':
                            print(f'We advise you to administer insulin soon')
                            break
                        elif administered == "h":
                            self.high_asking_for_help()
                            break

                    except:
                        print("It looks like there was an error of some kind")
            elif num < self.ideal_blood_suger - 30:
                print(f"Your Blood Sugar Level is to low {self.name}. Your Blood sugar level is currently at {num}. "
                      f"You have to eat or drink some carbs")
                while True:
                    try:
                        eat_drink = input(f"{self.name}, Have you eaten or drunk any carbs to bring your blood sugar "
                                          f"back up. Enter y for yes or n for no. If you need help enter h: ")
                        if eat_drink == 'y':
                            break
                        elif eat_drink == 'n':
                            print(f'We advise you to eat or drink some carbs soon')
                            break
                        elif eat_drink == 'h':
                            self.low_asking_for_help()
                            break

                    except:
                        print("It looks like there was an error of some kind")

Bob = Person("Bob",18,"Male","Type One")
Bob.calculate_insulin()
python-3.x oop
1个回答
0
投票

calculate_insulin应该计算胰岛素,不建议。可能您可以将其设为私有方法,并使用它来获取胰岛素值,然后从另一个方法check_insulin调用它以提供建议,并使该方法check_insulin公开。阅读面向OOP的SOLID原则。

© www.soinside.com 2019 - 2024. All rights reserved.