roberthsu2003 / __2024_07_01_Shilin_python__

士林python ai 初階
Apache License 2.0
13 stars 2 forks source link

請將下面的程式,使用亂數建立姓名,身高(150~190),體重(50~90) #9

Open roberthsu2003 opened 3 months ago

roberthsu2003 commented 3 months ago
import pyinputplus as pyip

class Person():
    def __init__(self,n:str,h:int,w:int):
        self.name = n
        self.height = h
        self.weight = w

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

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

    def bmi_print(self)->str:
        return f"{self.name}你好\n身高是:{self.height}公分\n體重是:{self.weight}公斤\nBMI:{round(self.getBmi(),ndigits=2)}\n{self.get_status()}"

    #name = pyip.inputStr("請輸入姓名:")
    #print(name,"\n")

    #height= pyip.inputNum("請輸入身高(cm):")
    #print(height,"\n")
    #weight = pyip.inputNum("請輸入體重(kg):")
    #print(weight,"\n")

    p = Person(name,height,weight)
    print(p.bmi_print())
Bowei0204 commented 3 months ago
import random
import pyinputplus as pyip
class Person():
    def __init__(self,n:str,h:int,w:int):
        self.name = n
        self.height = h
        self.weight = w

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

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

    def BMI_print(self):
        return f"""{self.name}你好
身高是:{self.height}公分
體重是:{self.weight}公斤
BMI:{round(self.getBMI(),ndigits=2)}
{self.get_status()}"""

file = open("names.txt",encoding="utf-8")
content = file.read()
file.close() 

while True: 
    names:list[str] = content.split()
    name = random.choices(names)  
    height = random.randint(150,190)
    weight = random.randint(50,90)

    p = Person(name,height,weight)
    print(p.BMI_print())

    answer=pyip.inputYesNo("離開(n)or繼續(y)\n")
    print()
    if answer=="no":
        break

print("應用程式結束")
heychi9487 commented 3 months ago
import pyinputplus as pyip

file = open('names.txt',encoding='utf-8')
content = file.read()
file.close()
import random
names:list[str] = content.split()
x=random.choices(names)

class Person():
    def __init__(self):
        self.name = x
        self.height = random.randint(150,190)
        self.weight = random.randint(50,90)

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

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

    def bmi_print(self)->str:
        return f"{self.name}你好\n身高是:{self.height}公分\n體重是:{self.weight}公斤\nBMI:{round(self.getBmi(),ndigits=2)}\n{self.get_status()}"

    #name = pyip.inputStr("請輸入姓名:")
    #print(name,"\n")

    #height= pyip.inputNum("請輸入身高(cm):")
    #print(height,"\n")
    #weight = pyip.inputNum("請輸入體重(kg):")
    #print(weight,"\n")

p = Person()
print(p.bmi_print())
Bergess0212 commented 3 months ago
import pyinputplus as pyip
import random

class Person():
    def __init__(self,n:str,h:int,w:int):
        self.name = n
        self.height = h
        self.weight = w

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

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

    def bmi_print(self)->str:
        return f"{self.name}你好\n身高是:{self.height}公分\n體重是:{self.weight}公斤\nBMI:{round(self.getBmi(),ndigits=2)}\n{self.get_status()}"

file = open("names.txt",encoding='utf-8')
content = file.read()
names:list[str] = content.split()
file.close()
height = random.randint(150,190)
weight = random.randint(50,90)

    #name = pyip.inputStr("請輸入姓名:")
    #print(name,"\n")

    #height= pyip.inputNum("請輸入身高(cm):")
    #print(height,"\n")
    #weight = pyip.inputNum("請輸入體重(kg):")
    #print(weight,"\n")

p = Person(random.choice(names),height,weight)
print(p.bmi_print())
sp11055422 commented 3 months ago
import random

class Person():

    def __init__(self,n:str,h:int,w:int):
        file=open('names.txt',encoding='utf-8')
        content=(file.read())
        file.close()
        names=content.split()
        self.name = random.choices(names)
        self.height =random.randint(150,190)
        self.weight =random.randint(50,90)

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

    def get_status(self) -> str:
        bmi:float = self.getBmi()
        if bmi >= 35:
            return '重度肥胖'
        elif bmi >= 30:
            return '中度肥胖'
        elif bmi >= 27:
            return '輕度肥胖'
        elif bmi >= 24:
            return '體重過重'
        elif bmi >= 18.5:
            return '體重正常'
        else:
            return '體重過輕'
    def bmi_print(self)->str:
        return f"{self.name}你好\n身高是:{self.height}公分\n體重是:{self.weight}公斤\nBMI:{round(self.getBmi(),ndigits=2)}\n{self.get_status()}"
file=open('names.txt',encoding='utf-8')
content=(file.read())
file.close()
names=content.split()
name = random.choices(names)
height =random.randint(150,190)
weight =random.randint(50,90)
p1=Person(name,height,weight)
print(p1.bmi_print())
Marcoke999 commented 3 months ago

import random
file = open('names.txt',encoding='utf-8')
content = file.read()

class Person():
    def __init__(self,n:str,h:int,w:int):
        self.name = n
        self.height = random.randint(150,190)
        self.weight = random.randint(50,90)
        n = random.choice(name)
    def getBmi(self) -> float:
        return self.weight / (self.height/100) ** 2

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

    def bmi_print(self)->str:
        return f"{self.name}你好\n身高是:{self.height}公分\n體重是:{self.weight}公斤\nBMI:{round(self.getBmi(),ndigits=2)}\n{self.get_status()}"

p = Person(name,height,weight)
print(p.bmi_print())```
Ron091 commented 3 months ago
import pyinputplus as pyip

class Person():
    def __init__(self,n:str,h:int,w:int):
        self.name = n
        self.height = h
        self.weight = w

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

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

    def bmi_print(self)->str:
        return f"{self.name}你好\n身高是:{self.height}公分\n體重是:{self.weight}公斤\nBMI:{round(self.getBmi(),ndigits=2)}\n{self.get_status()}"

def getPerson(name:str) -> Person:
    height = random.randint(150,190)
    weight = random.randint(50,90)
    return Person(n=name,h=height,w=weight)

with open('names.txt',encoding='utf-8',newline='') as file:
    contents:str = file.read()
    names:list[str] = contents.split(sep='\n')

name:str =random.choices(names)
P1=getPerson(name)

print(P1.bmi_print())