roberthsu2003 / __2024_04_17_mon_wed__

Python與AI人工智慧開發入門
25 stars 4 forks source link

請建立 DataClass BMI #15

Open roberthsu2003 opened 1 month ago

roberthsu2003 commented 1 month ago

建立class BMI

建立Fields->name,height,weight

建立property->bmi

建立實體方法def status(self) -> str

輸入

輸出

chesterXalan commented 1 month ago
from dataclasses import dataclass
import pyinputplus as pyip

@dataclass
class BMI():
    name: str
    height: float
    weight: float

    @property
    def bmi(self):
        return self.weight / (self.height / 100) ** 2

    def status(self) -> str:
        if self.bmi < 18.5:
            result = '過輕'
        elif self.bmi < 24:
            result = '正常'
        elif self.bmi < 27:
            result = '過重'
        elif self.bmi < 30:
            result = '輕度肥胖'
        elif self.bmi < 35:
            result = '中度肥胖'
        else:
            result = '重度肥胖'
        return result

name = input('請輸入姓名: ')
height = pyip.inputFloat('請輸入身高(cm): ', min=0)
weight = pyip.inputFloat('請輸入體重(kg): ', min=0)

p1 = BMI(name, height, weight)
print(f'\n{p1.name}您好:\n您的BMI值是: {p1.bmi:.2f}\n您的體重: {p1.status()}\n')

螢幕擷取畫面 2024-05-15 222148

MurrayChuang commented 1 month ago
from dataclasses import dataclass
import pyinputplus as pyip

name = input('請輸入姓名:')
height = pyip.inputFloat('請輸入身高(cm):', min=1, max=300)
weight = pyip.inputFloat('請輸入體重(kg):', min=1, max=500)

@dataclass
class BMI():
    name:str
    height:float
    weight:float

    @property
    def bmi(self):
        return weight / (height / 100)**2

    def status(self) -> str:
        bmi_value = self.bmi
        if bmi_value < 18.5:
            result = '過輕'
        elif bmi_value < 24:
            result = '正常'
        elif bmi_value < 27:
            result = '過重'
        elif bmi_value < 30:
            result = '輕度肥胖'
        elif bmi_value < 35:
            result = '中度肥胖'
        else:
            result = '重度肥胖'
        return result

user = BMI(name, height, weight)

print(f"{user.name}您好:")
print(f"您的bmi值是{user.bmi:.2f}")
print(f"您的體重:{user.status()}")
image
Tony840705 commented 1 month ago
from dataclasses import dataclass
import pyinputplus as pyip

name = input('請輸入姓名: ')
height = pyip.inputFloat('請輸入身高(120~230)(cm): ', min=120, max=230)
weight = pyip.inputFloat('請輸入體重(40~170)(kg): ', min=40, max=170)

@dataclass
class BMI():
    name : str
    height : float
    weight : float

    @property
    def bmi(self):
        return self.weight / (self.height / 100) ** 2

    def status(self):
        if self.bmi < 18.5:
            result = '過輕'
        elif self.bmi < 24:
            result = '正常'
        elif self.bmi < 27:
            result = '過重'
        elif self.bmi < 30:
            result = '輕度肥胖'
        elif self.bmi < 35:
            result = '中度肥胖'
        else:
            result = '重度肥胖'
        return result

A1 = BMI(name, height, weight)
print(f'\n{A1.name}您好:\n您的BMI值為:{A1.bmi:.2f}\n您的體重:{A1.status()}\n')

BMI_2

Tina54321 commented 1 month ago
from dataclasses import dataclass
import pyinputplus as pyip

@dataclass
class Bmi():
    name:str
    height:float
    weight:float

    @property
    def bmi(self):
        return self.weight / (self.height / 100) ** 2

    def status(self)->str:
        if self.bmi < 18.5:
            result = '過輕'
        elif self.bmi < 24:
            result = '正常'
        elif self.bmi < 27:
            result = '過重'
        elif self.bmi < 30:
            result = '輕度肥胖'
        elif self.bmi < 35:
            result = '中度肥胖'
        else:
            result = '重度肥胖'
        return result

n = input("請輸入姓名: ")
h = pyip.inputNum("請輸入身高(cm): ", greaterThan=0)
w = pyip.inputNum("請輸入體重(kg): ", greaterThan=0)
p1 = Bmi(n,h,w)
print(p1)
print(f"\n{p1.name}您好:\n您的BMI值是: {p1.bmi:.2f}\n您的體重: {p1.status()}")

image

ccanna commented 1 month ago
from dataclasses import dataclass

@dataclass
class BMI():
    name:str
    height:int
    weight:int

    def __repr__(self):
        message = f"請輸入姓名:{self.name}\n"
        message += f"請輸入身高:{self.height}\n"
        message += f"請輸入體重:{self.weight}\n"
        return message

    #建立property的語法
    @property
    def bmi(self):
        return self.weight / (self.height / 100) ** 2

 #以下還沒改

    #建立實體方法
    def status(self)->str:
        if self.bmi < 18.5:
            self.result = '過輕'
        elif self.bmi < 24:
            self.result = '正常'
        elif self.bmi < 27:
            self.result = '過重'
        elif self.bmi < 30:
            self.result = '輕度肥胖'
        elif self.bmi < 35:
            self.result = '中度肥胖'
        else:
            self.result = '重度肥胖'
        return self.result

s1= BMI("王小明",175,68)
print(s1)
print(f"您的BMI值是:{s1.bmi:.2f}" )
print(f"您的體重:{s1.status()}")

print('============================')

s2= BMI("Tom Wang",167,80)
print(s2)
print(f"您的BMI值是:{s2.bmi:.2f}" )
print(f"您的體重:{s2.status()}")

image

chihweihan commented 1 month ago
from dataclasses import dataclass

name = input("請輸入姓名:")
height = pyip.inputFloat("請輸入身高(cm):")
weight = pyip.inputFloat("請輸入體重(kg):")

@dataclass
class BMI():
    name:str
    height:float
    weight:float

    @property
    def bmi(self):
        return weight / (height / 100) ** 2

    # 建立實體方法
    def status(self)->str:
        bmi_value = self.bmi
        if bmi_value < 18.5:
            result = "體重過輕"
        elif bmi_value < 24:
            result = "正常範圍"
        elif bmi_value < 27:
            result = "體重過重"
        elif bmi_value < 30:
            result = "輕度肥胖"
        elif bmi_value < 35:
            result = "中度肥胖"
        else:
            result = "重度肥胖"
        return result

A1 = BMI(name, height, weight)
print(f"\n{A1.name},您好:")
print(f"您的bmi值是:{A1.bmi:.2f}")
print(f"體況屬於:{A1.status()}")

image

chiayuben commented 1 month ago
import pyinputplus as pyip
from dataclasses import dataclass

@dataclass
class BMI():
    name:str
    high:float
    weight:float

    @property
    def bmi(self):
        return self.weight / (self.high/100) ** 2

    def status(self)->str:
        if self.bmi < 18.5:
            result = '過輕'
        elif self.bmi < 24:
            result = '正常'
        elif self.bmi < 27:
            result = '過重'
        elif self.bmi < 30:
            result = '輕度肥胖'
        elif self.bmi < 35:
            result = '中度肥胖'
        else:
            result = '重度肥胖'
        return result

    def __repr__(self):  
        data = f"\n{self.name}您好:,您的BMI值是{self.bmi:.2f},您屬於:{self.status()}\n"
        return data

name = input("請輸入姓名: ")
high = pyip.inputFloat(f"請輸入身高(cm): ", min=1)
print(high)
weight = pyip.inputFloat(f"請輸入體重(kg):", min=1)
print(weight)
p1 = BMI(name,high,weight)
print(p1)

image

PercJK commented 1 month ago
from dataclasses import dataclass
import pyinputplus as pyip

@dataclass
class Bmi():
    name:str
    height:float
    weight:float

    @property
    def bmi(self):
        return self.weight / (self.height / 100) ** 2

    def status(self)->str:
        if self.bmi < 18.5:
            result = '過輕'
        elif self.bmi < 24:
            result = '正常'
        elif self.bmi < 27:
            result = '過重'
        elif self.bmi < 30:
            result = '輕度肥胖'
        elif self.bmi < 35:
            result = '中度肥胖'
        else:
            result = '重度肥胖'
        return result

n = input("請輸入姓名: ")
h = pyip.inputNum("請輸入身高(cm): ", greaterThan=0)
w = pyip.inputNum("請輸入體重(kg): ", greaterThan=0)
p1 = Bmi(n,h,w)
print(p1)
print(f"\n{p1.name}您好:\n您的BMI值是: {p1.bmi:.2f}\n您的體重: {p1.status()}")

lesson9

victor1629 commented 1 month ago
from dataclasses import dataclass

@dataclass
class BMI():
    name : str
    height : float
    weight : float

    @property
    def bmi(self):
        return self.weight / (self.height / 100) ** 2

    def status(self):
        if self.bmi < 18.5:
            result = "過輕"
        elif self.bmi < 24:
            result = "正常範圍"
        elif self.bmi < 27:
            result = "過重"
        elif self.bmi < 30:
            result = "輕度肥胖"
        elif self.bmi < 35:
            result = "中度肥胖"
        else:
            result = "重度肥胖"
        return result

import pyinputplus as pyip
name = input("請輸入姓名:")
print(f"請輸入姓名:{name}")
height = pyip.inputInt("請輸入身高(cm):")
print(height)
weight = pyip.inputInt("請輸入體重(kg):")
print(weight)

s1 = BMI(name,height,weight)
print(f'{s1.name}您好:')
print(f'您的bmi值是{s1.bmi:.2f}')
print(f'您的體重:{s1.status()}')
print("================")

螢幕擷取畫面 2024-05-20 175306

drama554 commented 1 month ago
from dataclasses import dataclass
import pyinputplus as pyip

@dataclass
class BMI():
    name: str
    height: float
    weight: float

    @property
    def bmi(self):
        return self.weight / (self.height / 100) ** 2

    def status(self) -> str:
        if self.bmi < 18.5:
            result = '過輕'
        elif self.bmi < 24:
            result = '正常'
        elif self.bmi < 27:
            result = '過重'
        elif self.bmi < 30:
            result = '輕度肥胖'
        elif self.bmi < 35:
            result = '中度肥胖'
        else:
            result = '重度肥胖'
        return result

name = input('請輸入姓名: ')
height = pyip.inputFloat('請輸入身高(cm): ', min=0)
weight = pyip.inputFloat('請輸入體重(kg): ', min=0)

p1 = BMI(name, height, weight)
print(f'\n{p1.name}您好:\n您的BMI值是: {p1.bmi:.2f}\n您的體重: {p1.status()}\n')

123

KIOVER998 commented 1 month ago
from dataclasses import dataclass
import pyinputplus as pyip

@dataclass
class BMI():
    name: str
    height: float
    weight: float

    @property
    def bmi(self):
        return self.weight / (self.height / 100) ** 2

    def status(self) -> str:
        if self.bmi < 18.5:
            result = '過輕'
        elif self.bmi < 24:
            result = '正常'
        elif self.bmi < 27:
            result = '過重'
        elif self.bmi < 30:
            result = '輕度肥胖'
        elif self.bmi < 35:
            result = '中度肥胖'
        else:
            result = '重度肥胖'
        return result

name = input('請輸入姓名: ')
height = pyip.inputFloat('請輸入身高(cm): ', min=0)
weight = pyip.inputFloat('請輸入體重(kg): ', min=0)

p1 = BMI(name, height, weight)
print(f'\n{p1.name}您好:\n您的BMI值是: {p1.bmi:.2f}\n您的體重: {p1.status()}\n')

擷取