首页 > 解决方案 > 如何在函数中适当地使用字典中的值

问题描述

我正在编写一段代码,但我似乎无法弄清楚如何在函数中使用字典中的项目。我希望输出以这种方式给出几行:

Christy is normal weight
Edward is obese
Helena is overweight
George is overweight
Sophia is normal weight

该函数必须包含参数性别、腰围和臀围。我尝试了几件事,但仍然无法正常工作。所以我试图让这段代码工作并提供所需的输出。

christy = {"name": "Christy", "sex": "female", "waist": 73, "hip": 93}
edward = {"name": "Edward", "sex": "male", "waist": 110, "hip": 110}
helena = {"name": "Helena", "sex": "female", "waist": 77, "hip": 92}
george = {"name": "George", "sex": "male", "waist": 91, "hip": 101}
sophia = {"name": "Sophia", "sex": "female", "waist": 76, "hip": 96}

people = [christy, edward, helena, george, sophia]

def waist_to_hip_ratio_2(sex, waist, hip):
    if sex == "female":
        waist1 = waist
        hip1 = hip
        whr = waist1 / hip1
        if whr < 0.8:
            return "person is normal weight"
        elif 0.8 <= whr <= 0.84:  
            return "person is overweight"
        else:
            return "person is obese"
    if sex == "male":
        waist2 = waist
        hip2 = hip
        whr = waist2 / hip2
        if whr < 0.9:
            return "person is normal weight"
        elif 0.9 <= whr <= 0.99:     
            return "person is overweight"
        else:
            return "person is obese"
    for person in people:
        if person == christy:
            name = christy.get("name")
            sex = christy.get("sex")
            waist = christy.get("waist")
            hip = christy.get("hip")
            print(name and waist_to_hip_ratio_2(sex, waist, hip))
        if person == edward:
            sex = edward.get("sex")
            waist = edward.get("waist")
            hip = edward.get("hip")
            print(name and waist_to_hip_ratio_2(sex, waist, hip))
        if person == helena:
            sex = helena.get("sex")
            waist = helena.get("waist")
            hip = helena.get("hip")
            print(name and waist_to_hip_ratio_2(sex, waist, hip))
        if person == george:
            sex = george.get("sex")
            waist = george.get("waist")
            hip = george.get("hip")
            print(name and waist_to_hip_ratio_2(sex, waist, hip))
        if person == sophia:
            sex = sophia.get("sex")
            waist = sophia.get("waist")
            hip = sophia.get("hip")
            print(name and waist_to_hip_ratio_2(sex, waist, hip))

标签: pythonfunctiondictionary

解决方案


您可以执行以下操作:

christy = {"name": "Christy", "sex": "female", "waist": 73, "hip": 93}
edward = {"name": "Edward", "sex": "male", "waist": 110, "hip": 110}
helena = {"name": "Helena", "sex": "female", "waist": 77, "hip": 92}
george = {"name": "George", "sex": "male", "waist": 91, "hip": 101}
sophia = {"name": "Sophia", "sex": "female", "waist": 76, "hip": 96}

people = [christy, edward, helena, george, sophia]

for person in people:
    if person['sex'] == 'male': # Set the thresholds depending on gender for each iteration
        a, b = 0.9, 0.99
    else:
        a, b = 0.8, 0.84
        
    if person['waist']/person['hip'] < a:
        print(f"{person['name']} is normal weight")
    elif b >= person['waist']/person['hip'] >= a:
        print(f"{person['name']} is overweight")
    else:
        print(f"{person['name']} is obese")

输出:

Christy is normal weight
Edward is obese
Helena is overweight
George is overweight
Sophia is normal weight

推荐阅读