roberthsu2003 / __2024_07_29_1_3_night__

2024_07_29_1_3_巨匠AI
20 stars 3 forks source link

請將以下程式改為class的寫法 #6

Open roberthsu2003 opened 3 weeks ago

roberthsu2003 commented 3 weeks ago

請自訂一個class叫BMI

class BMI():
    pass

while True:
    try:
        name = input("請輸入姓名: ")
        height = float(input('請輸入身高(cm): '))
        weight = float(input('請輸入體重(kg): '))
        #這裏建立一個BMI的實體

    except ValueError:
        print("格式錯誤,請重新輸入數據")
        continue

    stuff = input("請問是否繼續輸入資料 ('q': 離開, 任意鍵: 繼續)? ")

    if stuff == 'q':
        break

print("應用程式結束")
FriendlyLu commented 3 weeks ago
class BMI():
    def __init__(self, name:str, height:float, weight:float):
        self.name = name
        self.height = height
        self.weight = weight

    def getName(self)->str:
        return self.name

    def getBMI(self)->float:
        return round(self.weight / ((self.height * 0.01) ** 2), ndigits=2)

    def get_status_message(self)->str:
        self.bmi = self.getBMI()

        if self.bmi < 18.5:
            return "體重過輕"
        elif self.bmi < 24:
            return "正常範圍"
        elif self.bmi < 27:
            return "過重"
        elif self.bmi < 30:
            return "輕度肥胖"
        elif self.bmi < 35:
            return "中度肥胖"
        else:
            return "重度肥胖"

while True:
    try:
        name = input("請輸入姓名: ")
        height = float(input('請輸入身高(cm): '))
        weight = float(input('請輸入體重(kg): '))

        #這裏建立一個BMI的實體
        MyBMI = BMI(name=name, height=height, weight=weight)
        print(f"{MyBMI.getName()} 的 BMI 為{MyBMI.getBMI()}, 為{MyBMI.get_status_message()}")
    except ValueError:
        print("格式錯誤,請重新輸入數據")
        continue

    stuff = input("請問是否繼續輸入資料 ('q': 離開, 任意鍵: 繼續)? ")

    if stuff == 'q':
        break

print("應用程式結束")
hellno3009 commented 3 weeks ago
class BMI():
    def __init__(self, name:str, height:float, weight:float):
        self.name = name
        self.height = height
        self.weight = weight

    def getname(self) -> str:
        return self.name

    def getbmi(self) -> float:
        return self.weight / ((self.height * 0.01) ** 2)

    def get_status_message(self, bmi: float) -> str:
        if bmi < 18.5:
            return "體重過輕"
        elif bmi < 24:
            return "正常範圍"
        elif bmi < 27:
            return "過重"
        elif bmi < 30:
            return "輕度肥胖"
        elif bmi < 35:
            return "中度肥胖"
        else:
            return "重度肥胖"

    def result(self) -> str:
        bmi = self.getbmi()
        return f"{self.getname()} 的 BMI 為 {bmi:.2f}, 為 {self.get_status_message(bmi)}"

while True:
    try:
        name = input("請輸入姓名: ")
        height = float(input('請輸入身高(cm): '))
        weight = float(input('請輸入體重(kg): '))

        Mybmi = BMI(name=name, height=height, weight=weight)
        print(bmi.result())
    except ValueError:
        print("格式錯誤")
        continue

    stuff = input("是否繼續輸入資料?(q: 離開,其他鍵:繼續)")

    if stuff == 'q':
        break

print("程式結束")
Oscar-Lee-20211298 commented 3 weeks ago
class BMI():
    def __init__(self, name:str, height:float, weight:float):
        self.name = name
        self.height = height
        self.weight = weight

    def getName(self) -> str:
        return self.name

    def getBMI(self) -> float:
        return self.weight / (self.height ** 2)

    def get_status_message(self, bmi: float) -> str:

        if bmi<18.5:
            return "too thin"
        elif bmi<24:
            return "normal"
        elif bmi<27:
            return "heavy"
        elif bmi<30:
            return "simple heavy"
        elif bmi<35:
            return "normal heavy"
        else:
            return "seriously heavy"

    def result(self) -> str:
        bmi = self.getBMI()
        return f"{self.getName()} 's BMI is {bmi}, is {self.get_status_message(bmi)}"

while True:
    try:
        name=str(input("please enter your name"))
        height=float(input('please enter your height'))
        weight=float(input('please enter your weight'))

        Mybmi = BMI(name=name, height=height, weight=weight)
        print(Mybmi.result())

    except ValueError:
        print("format error")
        continue
    stuff = input("please enter next data ('q':quit)")

    if stuff == 'q':
        break
print("app is end")
Srdell7 commented 3 weeks ago
class BMI():
    def __init__(self,name:str,height:float,weight:float):
        self.name = name
        self.height = height
        self.weight = weight

    def bmi(self)->float:
        return round((self.weight/(self.height*0.01)**2),ndigits=2)

    def grade(self)->str:
        self.grade = self.bmi()

        if self.grade<18.5:
            return "體重過輕"
        elif self.grade<24:
            return "正常範圍"
        elif self.grade<27:
            return "過重"
        elif self.grade<30:
            return "輕度肥胖"
        elif self.grade<35:
            return "中度肥胖"
        elif self.grade>=35:
            return "重度肥胖"

    def description(self)->str:
        return f'您的名字:{self.name}\nBMI為:{self.bmi()}\n體重為:{self.grade()}'

while True:
    try:
        name = input("請輸入姓名: ")
        height = float(input('請輸入身高(cm): '))
        weight = float(input('請輸入體重(kg): '))
        yoursbmi = BMI(name=name,height=height,weight=weight)
        print(yoursbmi.description())

    except ValueError:
        print("格式錯誤,請重新輸入數據")
        continue

    stuff = input("請問是否繼續輸入資料 ('q': 離開, 任意鍵: 繼續)? ")
    if stuff == 'q':
        break
print("應用程式結束")
yawenny commented 2 weeks ago
class BMI():
    def __init__(self,n:str,h:float,w:float)->str:
        self.name = n
        self.weight = w
        self.height = h

    def description(self)->str:#實體方法
        return f'姓名:{self.name}\n身高:{self.height}cm\n體重:{self.weight}kg'

    def bmi(self)->float:
        return round(self.weight/(((self.height*0.01))**2),ndigits=2)

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

while True:
    try:
        name = input("請輸入姓名: ")
        height = float(input('請輸入身高(cm): '))
        weight = float(input('請輸入體重(kg): '))
        #這裏建立一個BMI的實體

        p1 =BMI(n=name,h=height,w=weight)#自訂的初始化,p1及p2實體
        print(p1.description())
        print(f'BMI值:{p1.bmi()}')
        print(f'結果:{p1.result()}\n')

    except ValueError:
        print("格式錯誤,請重新輸入數據")
        continue

    stuff = input("請問是否繼續輸入資料 ('q': 離開, 任意鍵: 繼續)? ")

    if stuff == 'q':
        break

print("應用程式結束")
handsomeyoxi commented 2 weeks ago
class BMI():
    def __init__(self, name:str, height:float, weight:float):
        self.name = name
        self.height = height
        self.weight = weight

    def getname(self) -> str:
        return self.name

    def calculate_BMI(self) -> float:
        return round((self.weight/(self.height*0.01)**2),ndigits=2)

    def health(self) -> str:
        bmi_value = self.calculate_BMI()

        if BMI < 18.5:
            print("您的體重過輕!")
        elif 18.5 <= BMI < 24:
            print("您的體重適中!")
        elif 24 <= BMI < 27:
            print("您的體重過重!")
        elif 27 <= BMI < 30:
            print("您的體重為輕度肥胖!")
        elif 30 <= BMI < 35:
            print("您的體重為中度肥胖!")
        else:
            print("您的體重為重度肥胖!")

    def result(self) -> str:
        BMI = self.getBMI()
        return f"{self.getname()} 的 BMI 為 {BMI:.2f}, 為 {self.get_status_message(BMI)}"

while True:
    try:
        name = input("請輸入姓名: ")
        height = float(input("請輸入身高(cm): "))
        weight = float(input("請輸入體重(kg): "))

        MyBMI = BMI(name=name, height=height, weight=weight)
        print(MyBMI.health())
    except ValueError:
        print("格式錯誤,請重新輸入數據")
        continue

    stuff = input("請問是否繼續輸入資料 ('q': 離開, 任意鍵: 繼續)? ")

    if stuff == "q":
        break

print("應用程式結束")
WayneChou-bot commented 2 weeks ago
class BMI():
    def __init__(self, name:str, height:float, weight:float):
        self.name = name
        self.height = height
        self.weight = weight

    def get_name(self)->str:
        return self.name

    def get_bmi(self)->float:
        return round(self.weight / ((self.height * 0.01) ** 2), ndigits=2)

    def get_status_message(self)->str:
        self.bmi = self.get_bmi()

        if self.bmi < 18.5:
            return "體重過輕"
        elif self.bmi < 24:
            return "正常範圍"
        elif self.bmi < 27:
            return "過重"
        elif self.bmi < 30:
            return "輕度肥胖"
        elif self.bmi < 35:
            return "中度肥胖"
        else:
            return "重度肥胖"

while True:
    try:
        name = input("請輸入姓名: ")
        height = float(input('請輸入身高(cm): '))
        weight = float(input('請輸入體重(kg): '))

        #這裏建立一個BMI的實體
        MyBMI = BMI(name=name, height=height, weight=weight)
        print(f"{MyBMI.get_name()} 的 BMI 為{MyBMI.get_bmi()}, 為{MyBMI.get_status_message()}")
    except ValueError:
        print("格式錯誤,請重新輸入數據")
        continue

    stuff = input("請問是否繼續輸入資料 ('q': 離開, 任意鍵: 繼續)? ")

    if stuff == 'q':
        break

print("應用程式結束")
grace588 commented 2 weeks ago
class BMI():
    def __init__(self, name:str, height:float, weight:float):
        self.name = name
        self.height = height
        self.weight = weight

    def getName(self)->str:
        return self.name

    def getBMI(self)->float:
        return round(self.weight / ((self.height * 0.01) ** 2), ndigits=2)

    def get_status_message(self)->str:
        self.bmi = self.getBMI()

        if self.bmi < 18.5:
            return "體重過輕"
        elif self.bmi < 24:
            return "正常範圍"
        elif self.bmi < 27:
            return "過重"
        elif self.bmi < 30:
            return "輕度肥胖"
        elif self.bmi < 35:
            return "中度肥胖"
        else:
            return "重度肥胖"

while True:
    try:
        name = input("請輸入姓名: ")
        height = float(input('請輸入身高(cm): '))
        weight = float(input('請輸入體重(kg): '))

        #這裏建立一個BMI的實體
        BMI = BMI(name=name, height=height, weight=weight)
        print(f"{BMI.getName()} 的 BMI 為{BMI.getBMI()}, 為{BMI.get_status_message()}")
    except ValueError:
        print("格式錯誤,請重新輸入數據")
        continue

    stuff = input("請問是否繼續輸入資料 ('q': 離開, 任意鍵: 繼續)? ")

    if stuff == 'q':
        break

print("應用程式結束")
SophiaSu0925 commented 2 weeks ago
class BMI():
  def __init__(self,name:str,height:float,weight:float):
    self.name = name
    self.height = height
    self.weight = weight
  def get_bmi(self)->float:
    return round(self.weight/(self.height*0.01)**2, ndigits = 2)
  def get_status_message(self)->str:
    bmi = self.get_bmi()
    if bmi < 18.5:
        return "體重過輕"
    elif bmi < 24:
        return "正常範圍"
    elif bmi < 27:
        return "過重"
    elif bmi < 30:
        return "輕度肥胖"
    elif bmi < 35:
        return "中度肥胖"
    else:
        return "重度肥胖"
  def result(self)->str:
    return f"{self.name}的BMI為{self.get_bmi()},為{self.get_status_message()}"

while True:
  try:
    name = str(input("請輸入姓名:"))
    height = float(input("請輸入身高:"))
    weight = float(input("請輸入體重:"))
    bmi = BMI(name,height,weight)
    print(bmi.result())

  except ValueError:
        print("格式錯誤,請重新輸入數據")
        continue
  stuff = input("請問是否繼續輸入資料('q':離開,任意鍵:繼續)?")
  if stuff == 'q':
        break

print("應用程式結束")