roberthsu2003 / __2024_10_15__gjun_ai__

巨匠2024 10 15 AI初階 晚上2_4班
27 stars 2 forks source link

請將下面的程式碼,傳出的list[BMI],儲存為bmi.csv檔 #8

Open roberthsu2003 opened 1 day ago

roberthsu2003 commented 1 day ago

bmi.csv的欄位名稱為:姓名,身高,體重,BMI,狀態

import random
class BMI(): 
    def __init__(self,name:str,height:int,weight:int): 
        self.name = name
        self.height = height
        self.weight = weight

    def get_bmi(self)->float: #取得bmi
        return round(self.weight / (self.height/100) ** 2,ndigits=2)

    def get_status(self)->str:  #取得status
        bmi = self.get_bmi()
        if bmi >=35:
            status = "您的體重:重度肥胖"
        elif bmi >= 30:
            status = "您的體重:中度肥胖"
        elif bmi >= 27:
            status = "您的體重:輕度肥胖"
        elif bmi >= 24:
            status = "您的體重:過重"
        elif bmi >= 18.5:
            status = "您的體重:正常範圍"
        else:
            status = "您的體重:體重過輕"
        return status

def get_name(nums:int=2)->list[str]: #抓取name.txt裡面的名字
    with open('names.txt',mode='r',encoding='utf-8') as file:
            names_str = file.read()
    names = names_str.split()
    names = random.choices(names,k=nums)
    return  names

def generate_bmi(names:list[str])->list[BMI]: 
    students:list[BMI] = []
    for name in names:
        height = random.randint(140,190)
        weight = random.randint(50,110)
        student_STATUS = BMI(name, height, weight)
        students.append(student_STATUS) #list實體化 
    return students

if __name__ == '__main__':
    nums = int(input('請輸入人數:'))
    names:list[str] = get_name(nums=nums)
    students:list[BMI] = generate_bmi(names=names)
    for student in students:
        print(f"姓名:{student.name}")
        print(f"身高:{student.height}")
        print(f"體重:{student.weight}")
        print(f"BMI:{student.get_bmi()}")
        print(f"狀態:{student.get_status()}")
        print("===========================")
user-frank commented 1 day ago
import random , csv

class BMI(): 
    def __init__(self,name:str,height:int,weight:int): 
        self.name = name
        self.height = height
        self.weight = weight

    def get_bmi(self)->float: #取得bmi
        return round(self.weight / (self.height/100) ** 2,ndigits=2)

    def get_status(self)->str:  #取得status
        bmi = self.get_bmi()
        if bmi >=35:
            status = "您的體重:重度肥胖"
        elif bmi >= 30:
            status = "您的體重:中度肥胖"
        elif bmi >= 27:
            status = "您的體重:輕度肥胖"
        elif bmi >= 24:
            status = "您的體重:過重"
        elif bmi >= 18.5:
            status = "您的體重:正常範圍"
        else:
            status = "您的體重:體重過輕"
        return status

def get_name(nums:int=2)->list[str]: #抓取name.txt裡面的名字
    with open('names.txt',mode='r',encoding='utf-8') as file:
            names_str = file.read()
    names = names_str.split()
    names = random.choices(names,k=nums)
    return  names

def generate_bmi(names:list[str])->list[BMI]: 
    students:list[BMI] = []
    for name in names:
        height = random.randint(140,190)
        weight = random.randint(50,110)
        student_STATUS = BMI(name, height, weight)
        students.append(student_STATUS) #list實體化 
    return students

if __name__ == '__main__':
    nums = int(input('請輸入人數:'))
    names:list[str] = get_name(nums=nums)
    students:list[BMI] = generate_bmi(names=names)
    file_dict:list[dict] = []
    for student in students:
        print(f"姓名:{student.name}")
        print(f"身高:{student.height}")
        print(f"體重:{student.weight}")
        print(f"BMI:{student.get_bmi()}")
        print(f"狀態:{student.get_status()}")
        print("===========================")

       #將資料轉換成字典
        student_dict = {
        "姓名": student.name,
        "身高": student.height,
        "體重": student.weight,
        "BMI": student.get_bmi(),
        "狀態": student.get_status()
        }
        file_dict.append(student_dict)  #寫入list[dict]

print(file_dict) #測試

# bmi.csv的欄位名稱為:姓名,身高,體重,BMI,狀態
# 存成csv檔
with open('bmi.csv',mode='w+',encoding='utf-8',newline="") as file:
   dict_writer = csv.DictWriter(file,['姓名',"身高",'體重','BMI','狀態'])
   dict_writer.writeheader()
   dict_writer.writerows(file_dict) 

image

kuroneko1202 commented 1 day ago
import random
import csv
class BMI(): 
    def __init__(self,name:str,height:int,weight:int): 
        self.name = name
        self.height = height
        self.weight = weight

    def get_bmi(self)->float: #取得bmi
        return round(self.weight / (self.height/100) ** 2,ndigits=2)

    def get_status(self)->str:  #取得status
        bmi = self.get_bmi()
        if bmi >=35:
            status = "您的體重:重度肥胖"
        elif bmi >= 30:
            status = "您的體重:中度肥胖"
        elif bmi >= 27:
            status = "您的體重:輕度肥胖"
        elif bmi >= 24:
            status = "您的體重:過重"
        elif bmi >= 18.5:
            status = "您的體重:正常範圍"
        else:
            status = "您的體重:體重過輕"
        return status

def get_name(nums:int=2)->list[str]: #抓取name.txt裡面的名字
    with open('names.txt',mode='r',encoding='utf-8') as file:
            names_str = file.read()
    names = names_str.split()
    names = random.choices(names,k=nums)
    return  names

def generate_bmi(names:list[str])->list[BMI]: 
    students:list[BMI] = []
    for name in names:
        height = random.randint(140,190)
        weight = random.randint(50,110)
        student_STATUS = BMI(name, height, weight)
        students.append(student_STATUS) #list實體化 
    return students

def save_to_csv(students:list[BMI]):  #寫成csv檔
    with open('bmi.csv', mode='w',encoding='utf-8',newline='') as file:
        csv_writer = csv.DictWriter(file,['姓名','身高','體重','BMI','狀態'])
        csv_writer.writeheader()   

        for student in students:
            csv_writer.writerow({
                '姓名':student.name,
                '身高':student.height,
                '體重':student.weight,
                'BMI':student.get_bmi(),
                '狀態':student.get_status()
                })

if __name__ == '__main__':
    nums = int(input('請輸入人數:'))
    names:list[str] = get_name(nums=nums)
    students:list[BMI] = generate_bmi(names=names)
    save_to_csv(students)
    for student in students:
        print(f"姓名:{student.name}")
        print(f"身高:{student.height}")
        print(f"體重:{student.weight}")
        print(f"BMI:{student.get_bmi()}")
        print(f"狀態:{student.get_status()}")
        print("===========================")

3 4

xxxung commented 16 hours ago
import random
import csv

def get_names(naums:int=2)->list[str]:
    with open('names.txt',mode='r',encoding='utf-8') as file:
        namse_str = file.read()
    names = namse_str.split(sep='\n')
    names = random.choices(names,k=nums)
    return names

class BMI():
    def __init__(self,name:str,height:int,weight:int):
        self.name = name
        self.height = height
        self.weight = weight

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

    def get_status(self)->str:
        bmi = self.get_bmi()
        if bmi >= 35:
            status = "重度肥胖"
        elif bmi >= 30:
            status = "中度肥胖"
        elif bmi >= 27:
            status = "輕度肥胖"
        elif bmi >= 24:
            status = "過重"
        elif bmi >= 18.5:
            status = "正常範圍"
        else:
            status = "體重過輕"
        return status        

def get_bmi(names:list[str])->list[BMI]:
    students:list[BMI] = []
    for name in names :
        height = random.randint(150,210)
        weight = random.randint(60,110)
        stu_bmi = BMI(name=name , height=height ,weight=weight)
        students.append(stu_bmi)
    return students

if __name__ == '__main__':
    try:
        nums = int(input("請輸入數量:"))
        names:list[str] = get_names(naums=nums)
        students:list[BMI] = get_bmi(names=names)
        student_dict:list[dict] = []
        for student in students:
            print(f'姓名:{student.name}')
            print(f'身高:{student.height}')
            print(f'體重:{student.weight}')
            print(f'BMI:{student.get_bmi()}')
            print(f'狀態:{student.get_status()}')
            print('='*20)
            dict = {
                '姓名':student.name,
                '身高(cm)':student.height,
                '體重(kg)':student.weight,
                'BMI':student.get_bmi(),
                '狀態':student.get_status()
            }
            student_dict.append(dict)
        with open('bmi.csv',mode='w',encoding='utf-8',newline='') as file:
            writer = csv.DictWriter(file,fieldnames=['姓名','身高(cm)','體重(kg)','BMI','狀態'])
            writer.writeheader()
            writer.writerows(student_dict)
    except:
        print("輸入格式錯誤")

image image

cloudorz0525 commented 7 hours ago
import random
import csv
class BMI(): 
    def __init__(self,name:str,height:int,weight:int): 
        self.name = name
        self.height = height
        self.weight = weight

    def get_bmi(self)->float: #取得bmi
        return round(self.weight / (self.height/100) ** 2,ndigits=2)

    def get_status(self)->str:  #取得status
        bmi = self.get_bmi()
        if bmi >=35:
            status = "您的體重:重度肥胖"
        elif bmi >= 30:
            status = "您的體重:中度肥胖"
        elif bmi >= 27:
            status = "您的體重:輕度肥胖"
        elif bmi >= 24:
            status = "您的體重:過重"
        elif bmi >= 18.5:
            status = "您的體重:正常範圍"
        else:
            status = "您的體重:體重過輕"
        return status

def get_name(nums:int=2)->list[str]: #抓取name.txt裡面的名字
    with open('names.txt',mode='r',encoding='utf-8') as file:
            names_str = file.read()
    names = names_str.split()
    names = random.choices(names,k=nums)
    return  names

def generate_bmi(names:list[str])->list[BMI]: 
    students:list[BMI] = []
    for name in names:
        height = random.randint(140,190)
        weight = random.randint(50,110)
        student_STATUS = BMI(name, height, weight)
        students.append(student_STATUS) #list實體化 
    return students

if __name__ == '__main__':
    nums = int(input('請輸入人數:'))
    names:list[str] = get_name(nums=nums)
    students:list[BMI] = generate_bmi(names=names)
    file_dict:list[dict] = []
    for student in students:
        print(f"姓名:{student.name}")
        print(f"身高:{student.height}")
        print(f"體重:{student.weight}")
        print(f"BMI:{student.get_bmi()}")
        print(f"狀態:{student.get_status()}")
        print("===========================")
        student_dict={'姓名':student.name,'身高':student.height,'體重':student.weight,'BMI':student.get_bmi(),'狀態':student.get_status()}
        file_dict.append(student_dict)
    print(file_dict)    

with open('bmi.csv',mode='w',encoding='utf-8',newline="") as file:
   dict_writer = csv.DictWriter(file,['姓名',"身高",'體重','BMI','狀態'])
   dict_writer.writeheader()
   dict_writer.writerows(file_dict) 

2024112201 2024112202