roberthsu2003 / __2024_10_15__gjun_ai__

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

請建立巢狀資料結構:list[dict] #6

Open roberthsu2003 opened 1 week ago

roberthsu2003 commented 1 week ago
請輸入人數:5
姓名:xxxx
身高:xxx公分
體重:xxxx公斤
bmi:xx
狀態:xxxxx
=================
姓名:xxxx
身高:xxx公分
體重:xxxx公斤
bmi:xx
狀態:xxxxx
=================
姓名:xxxx
身高:xxx公分
體重:xxxx公斤
bmi:xx
狀態:xxxxx
=================
姓名:xxxx
身高:xxx公分
體重:xxxx公斤
bmi:xx
狀態:xxxxx
=================
姓名:xxxx
身高:xxx公分
體重:xxxx公斤
bmi:xx
狀態:xxxxx
=================
def get_names(nums:int=2)->list[str]:
      pass

def generate_bmi(names:list[str])->list[dict]:
     pass
截圖 2024-11-14 晚上9 29 32
xxxung commented 1 week ago

建立巢狀資料結構:list[dict]

import random
def get_name(nums:int=2)->list[str]:
    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[dict]:
    students:list[dict] = []
    for name in names:
        height = random.randint(140,190)
        weight = random.randint(50,110)
        BMI = weight / ((height/100)**2)
        if BMI >= 35:
            status = "重度肥胖"
        elif BMI >= 30:
            status = "中度肥胖"
        elif BMI >= 27:
            status = "輕度肥胖"
        elif BMI >= 24:
            status = "過重"
        elif BMI >= 18.5:
            status = "正常範圍"
        else:
            status = "體重過輕"
        student = {
            '姓名':name,
            '身高':height,
            '體重':weight,
            'BMI':round(BMI,2),
            '狀態':status
        }
        students.append(student)
    return students

nums = int(input('請輸入人數:'))
students = generate_bmi(get_name(nums=nums))
for name in students:
    for key,value in name.items():
        print(f'{key}:{value}')
    print('=========================')

image

kuroneko1202 commented 1 week ago
import random
def get_names(nums:int=2)->list[str]:
    with open("names.txt",mode="r",encoding="utf-8") as file:
        names_str = file.read()
    names:list[str] = names_str.split(sep='\n')
    names = random.choices(names,k=nums)
    return names

def generate_bmi(names:list[str]) ->list[dict]:
    student_status:list[dict] = []     #生成一個空的list
    for name in names:
        height = random.randint(140,190)    # 生成身高140~190公分
        weight = random.randint(50,110)     # 生成體重50~110公斤
        BMI = weight / ((height/100)**2)    # 計算bmi

        if BMI >=35:                        
            status = "重度肥胖"
        elif BMI >= 30:
            status = "中度肥胖"
        elif BMI >= 27:
            status = "輕度肥胖"
        elif BMI >= 24:
            status = "過重"
        elif BMI >= 18.5:
            status = "正常範圍"
        else:
            status = "體重過輕" 

        student = {"姓名": name,"身高":height,"體重":weight,"BMI":f"{BMI:.2f}","狀態":status}
        student_status.append(student)

    return student_status

nums = int(input("請輸入人數")) #取得輸入的數量
print(f"請輸入人數:{nums}")

student_names:list[str] = get_names(nums=nums) #取得剛剛輸入數量人數的姓名

student_status:list[dict] = generate_bmi(names=student_names) #取得輸入人數的各種狀態的資料
for student in student_status:
    print(f"姓名: {student['姓名']}")  #student['姓名']:name  key:value
    print(f"身高: {student['身高']}")
    print(f"體重: {student['體重']}")
    print(f"BMI:  {student['BMI']}")
    print(f"狀態: {student['狀態']}")
    print("======================")  

2

user-frank commented 1 week ago
import random

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

def generate_bmi(names:list[str])->list[dict]:
    personnel:list[dict] =[]
    for student in names :
        height = random.randint(140,190) #身高
        weight = random.randint(50,110) #體重
        bmi    = random.randint(17,40) #bmi
        if bmi >= 35 :
            BMI_category = "重度肥胖"
        elif bmi >= 30:
            BMI_category = "中度肥胖"
        elif bmi >= 27:
            BMI_category = "輕度肥胖"
        elif bmi >= 24:
            BMI_category = "過重"  
        elif bmi >= 18.5:
            BMI_category = "正常範圍"
        else:
            BMI_category = "體重過輕"
        score = {"姓名":student,"身高":height,"體重":weight,"bmi":bmi,"狀態":BMI_category}        
        personnel.append(score)                
    return personnel

nums = int(input("請輸入學生的數量(最多十位)"))
nums:list[str] = get_names(nums=nums)
personnel:list[dict] = generate_bmi(names=nums)

print(f"{'姓名         '}{'身高      '}{'體重        '}{'BMI         '}{'狀態     '}")
print("="*50)
for student in personnel:
    print(f"{student['姓名']}\
        {student['身高']}\
        {student['體重']}\
        {student['bmi']}\
        {student['狀態']}"
        )    

image

Vito8531 commented 6 days ago
import random

def get_names(nums):
    with open("names.txt","r",encoding="utf-8") as file:
        names_str = file.read()
    names = names_str.split(sep="\n")
    stu_names = random.choices(names,k=nums)
    return stu_names

def generate_bmi(names):
    students=[]
    for name in names:
        height = random.randint(140,190)
        weight = random.randint(50,110)
        BMI = weight / (height/100)**2
        if BMI >=35:
            status = "重度肥胖"
        elif BMI >= 30:
            status = "中度肥胖"
        elif BMI >= 27:
            status = "輕度肥胖"
        elif BMI >= 24:
            status = "過重"
        elif BMI >= 18.5:
            status = "正常範圍"
        else:
            status = "體重過輕"

        stu = {"姓名":name,"身高":height,"體重":weight,"BMI":round(BMI,2),"狀態":status}
        students.append(stu)
    return students

nums = int(input("請輸入人數:"))
print(f"請輸入人數:{nums}")
student_names = get_names(nums=nums)
#print(student_names)
student_bmi = generate_bmi(names=student_names)
for student in student_bmi:
    print(f"姓名 : {student['姓名']}")
    print(f"身高 : {student['身高']}")
    print(f"體重 : {student['體重']}")
    print(f"BMI : {student['BMI']}")
    print(f"狀態 : {student['狀態']}")
    print("=======================")

HW5

cloudorz0525 commented 6 days ago
import random
def get_names(nums:int=2)->list[str]:
    with open("names.txt","r",encoding="utf-8") as file:
        names_str = file.read()
    names = names_str.split(sep="\n")
    names =random.choices(names,k=nums)
    return names
def generate_bmi(names:list[str])->list[dict]:
    students=[]
    for name in names:
        height = random.randint(140,190)
        weight = random.randint(50,110)
        BMI = weight / (height/100)**2
        if   BMI >=35:
          status="重度肥胖"
        elif BMI >= 30:
          status="中度肥胖"
        elif BMI >= 27:
          status="輕度肥胖"
        elif BMI >= 24:
          status="過重"
        elif BMI >= 18.5:
          status="正常範圍"
        else:
          status="體重過輕"

        stu = {"姓名":name,"身高":height,"體重":weight,"BMI":f"{BMI:.2f}","狀態":status}
        students.append(stu)
    return students
nums = int(input("請輸入人數:"))
student_names = get_names(nums=nums)
student_bmi = generate_bmi(names=student_names)
for student in student_bmi:
    print(f"姓名 : {student['姓名']}")
    print(f"身高 : {student['身高']}")
    print(f"體重 : {student['體重']}")
    print(f"BMI : {student['BMI']}")
    print(f"狀態 : {student['狀態']}")
    print("=======================")

20241115

shinmaple commented 5 days ago
import random
def get_names(nums:int = 2)->list[str]:
    with open('names.txt', mode='r', encoding = 'utf-8') as f:  
        names_str = f.read()
    names:list[str] = names_str.split(sep= '\n')
    names_list = random.choices(names,k=nums)
    return names_list

def generate_students(names:list[str])->list[dict]:
    students:list[dict] = [] 
    for name in names:
        #print(f'姓名:{name}')
        height = random.randint(140, 190)
        #print(f'身高:{height}公分')
        weight = random.randint(50, 110)
        #print(f'體重:{weight}公斤')
        BMI = weight / ((height/100)**2) 
        #print(f'BMI:{BMI:.0f}')
        status=get_bmistatus(BMI)
        #print(f'狀態:{status}')
        #print("===================")
        student={'name':name,'height':height,'weight':weight,'BMI':format(BMI,'.0f'),'status':status}
        students.append(student)
    return students

def get_bmistatus(BMI:int)->str: 
    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
nums = int(input('請輸入人數:)'))
student_names:list[str] = get_names(nums = nums)
print(f'請輸入人數:{nums}')
s1 = generate_students(names = student_names)
for name in s1:
    print(f"姓名: {name['name']}")
    print(f"身高: {name['height']}")
    print(f"體重: {name['weight']}")
    print(f"BMI: {name['BMI']}")
    print(f"狀態: {name['status']}")
    print("=================")

未命名

Bellelin27 commented 5 days ago
import random
def get_names(nums:int=2)->list[str]:
    with open("names.txt",mode="r",encoding="utf-8") as file:
        names_str = file.read()
    names:list[str] = names_str.split(sep='\n')
    names = random.choices(names,k=nums)
    return names

def generate_bmi(names:list[str]) ->list[dict]:
    student_status:list[dict] = []     
    for name in names:
        height = random.randint(140,190)    
        weight = random.randint(50,110)     
        BMI = weight / ((height/100)**2)    

        if BMI >=35:                        
            status = "重度肥胖"
        elif BMI >= 30:
            status = "中度肥胖"
        elif BMI >= 27:
            status = "輕度肥胖"
        elif BMI >= 24:
            status = "過重"
        elif BMI >= 18.5:
            status = "正常範圍"
        else:
            status = "體重過輕" 

        student = {"姓名": name,"身高":height,"體重":weight,"BMI":f"{BMI:.2f}","狀態":status}
        student_status.append(student)

    return student_status

nums = int(input("請輸入人數")) 
print(f"請輸入人數:{nums}")

student_names:list[str] = get_names(nums=nums)

student_status:list[dict] = generate_bmi(names=student_names) 
for student in student_status:
    print(f"姓名: {student['姓名']}")  
    print(f"身高: {student['身高']}")
    print(f"體重: {student['體重']}")
    print(f"BMI:  {student['BMI']}")
    print(f"狀態: {student['狀態']}")
    print("======================")  
1114
benhuang1998 commented 4 days ago
import random
def get_names(nums:int=2)->list[str]:
    with open('names.txt',mode = 'r',encoding='utf-8') as file:
        names_str = file.read()
    names:list[str] = names_str.split(sep='\n')
    names = random.choices(names,k=nums)
    return names

def generate_students(names:list[str]) ->list[dict]:
    students:list[dict]=[]
    for name in names:
        height = random.randint(140,190)
        weight = random.randint(50,110)
        bmi = weight /((height/100)**2)
        if bmi >= 35:
            status = "重度肥胖"
        elif bmi >= 30:
            status = "中度肥胖"
        elif bmi >= 27:
            status = "輕度肥胖"
        elif bmi >= 24:
            status = "過重"
        elif bmi >= 18.5:
            status = "正常"
        else:
            status = "過輕"
        student ={'姓名':name,'身高':height,'體重':weight,'BMI':bmi,'狀態':status}
        students.append(student)
    return students

nums = int(input("請輸入人數1~10:"))
student_names = get_names(nums=nums)
student_bmi = generate_students(names=student_names)
for student in student_bmi:
    print(f"姓名 : {student['姓名']}\n",
        f"身高 : {student['身高']}\n",
        f"體重 : {student['體重']}\n",
        f"BMI : {student['BMI']}\n",
        f"狀態 : {student['狀態']}",)
    print("=======================")

螢幕擷取畫面 2024-11-18 102349 螢幕擷取畫面 2024-11-18 102401

rosaliehuang22 commented 4 days ago
import random

# Function to read name from file and choose random
def get_names(nums:int=2)->list[str]:
    with open('names.txt', 'r', encoding='utf-8') as file:
        names = file.read().splitlines() # Read each line
    return random.sample(names, k=nums) # Randomly select without duplicates

# Function to calculate health status based on BMI
def get_status(bmi:int):
    if bmi >=35:
        return "重度肥胖"
    elif bmi >= 30:
        return "中度肥胖"
    elif bmi >= 27:
        return "輕度肥胖"
    elif bmi >= 24:
        return "過重"
    elif bmi >= 18.5:
        return "正常範圍"
    else:
        return "體重過輕"

# Function to create a list of students with BMI and status
def generate_bmi(names:list[str])->list[dict]:
    students = []
    for name in names:
        height = random.randint(150,190)
        weight = random.randint(40,100)
        bmi = round(weight / ((height / 100) ** 2), 2)
        status = get_status(bmi)

        student = {
            'name': name,
            'height': height,
            'weight': weight,
            'bmi': bmi,
            'status': status
        }

        students.append(student)
    return students

nums = int(input('請輸入人數: '))
names = get_names(nums=nums) # Get the list of names
students = generate_bmi(names) # Create student list with BMI information
print(f"請輸入人數:{nums}")

# Print each student's information
for student in students:
    print(f"姓名 : {student['name']}")
    print(f"身高 : {student['height']} 公分")
    print(f"體重 : {student['weight']} 公斤")
    print(f"BMI : {student['bmi']}")
    print(f"狀態 : {student['status']}")
    print("=======")

image

nekoya10 commented 4 days ago

螢幕擷取畫面 2024-11-18 154836

建立巢狀資料結構

import random
def get_names(nums:int=2)->list[str]:
    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[dict]:
    students:list[dict] = [] 
    for name in names:
        height = random.randint(140,190)    # 隨機生成身高:140~190公分
        weight = random.randint(50,110)     # 隨機生成體重:50~110公斤
        BMI = weight / ((height/100)**2)    # 計算bmi

        if BMI >= 35:
            status = "重度肥胖"
        elif BMI >= 30:
            status = "中度肥胖"
        elif BMI >= 27:
            status = "輕度肥胖"
        elif BMI >= 24:
            status = "過重"
        elif BMI >= 18.5:
            status = "正常範圍"
        else:
            status = "體重過輕"

        student = {
            "姓名":name,
            "身高":height,
            "體重":weight,
            "BMI":round(BMI,2),
            "狀態":status
        }
        students.append(student)
    return students

nums = int(input("請輸入人數"))
print(f"請輸入人數:{nums}")
students = generate_bmi(get_names(nums=nums))
for name in students:
    for key,value in name.items():
        print(f'{key}:{value}')
    print("========================")
TingYi0921 commented 4 days ago
import random

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

nums = int(input('請輸入人數(最多10位):'))
people_names:list[str] = get_names(nums=nums)

def generate_BMI(names:list[str]) -> list[dict]:
    peoples:list[dict] = []
    for name in names:
        height = random.randint(140, 200)
        weight = random.randint(40, 100)
        BMI = weight / ((height/100)**2)
        def get_status(BMI:float)->str:
            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
        people = {'姓名:':name,'身高:':height,'體重:':weight,'BMI:':round(BMI,ndigits=2),'狀態:':get_status(BMI)}
        peoples.append(people)
    return peoples

print(f'請輸入人數:{nums}')
peoples = generate_BMI(names=people_names)
for people in peoples:
    for key, value in people.items():
        print(f'{key}{value}')
    print("================================")

巢狀資料結構程式1 巢狀資料結構程式2 巢狀資料結構程式3 巢狀資料結構輸出

xingxing8787 commented 4 days ago
import random

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

def generate_BMI(names: list[str]) -> list[dict]:
    students: list[dict] = []
    for name in names:
        weight = random.randint(40, 120)
        height = random.randint(140, 190)
        BMI = weight / ((height/100)**2)

        if BMI < 18.5:
            status = '體重過輕'
        elif 18.5 <= BMI < 24:
            status = '正常範圍'
        elif 24 <= BMI < 27:
            status = '過重'
        elif 27 <= BMI < 30:
            status = '輕度肥胖'
        elif 30 <= BMI < 35:
            status = '中度肥胖'
        else:
            status = '重度肥胖'

        student = {'姓名': name, '體重': weight, '身高': height, 'BMI': f"{BMI:.2f}", '狀態': status}
        students.append(student)

    return students

nums = int(input('請輸入學生數量(最多10位):'))
print(f"人數:{nums}")
student_names: list[str] = get_names(nums=nums)
students: list[dict] = generate_BMI(names=student_names)
for s1 in students:
    print(f"姓名: {s1['姓名']}")
    print(f"身高: {s1['身高']}")
    print(f"體重: {s1['體重']}")
    print(f"BMI:  {s1['BMI']}")
    print(f"狀態: {s1['狀態']}")
    print("======================")

{B8409F40-7F20-4F2A-8823-117E647F091D}

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

def generate_students(names:list[str]) -> list[dict]:
    students:list[dict] = []
    for name in names:
        height = random.randint(140,190)
        weight = random.randint(50,110)
        BMI = weight / ((height/100)**2)
        status = get_status(BMI)
        student = {'名字': name, '身高': height, '重量': weight, 'BMI': BMI, '狀態':status}
        students.append(student)
    return students

def get_status(BMI:int)->str:#有傳出值的function
    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 

nums = int(input('請輸入學生數量:'))
student_names:list[str] = get_names(nums=nums)
students = generate_students(names=student_names)
for name in students:
    for key,value in name.items():
        print(f'{key}:{value}')
    print('=========================')

image