首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在函数中适当地使用字典中的值

如何在函数中适当地使用字典中的值
EN

Stack Overflow用户
提问于 2020-07-05 18:38:45
回答 3查看 40关注 0票数 0

我正在编写一段代码,我似乎不知道如何在函数中使用字典中的条目。我希望输出以这种方式给出几行:

代码语言:javascript
运行
复制
Christy is normal weight
Edward is obese
Helena is overweight
George is overweight
Sophia is normal weight

功能必须包含性别、腰部和臀部的参数。我试过几件事,但还是没有用。因此,我试图让这段代码正常工作,并给出所需的输出。

代码语言:javascript
运行
复制
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))
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-07-05 18:56:09

下面是我对您的代码所做的更改:

  1. 将"person is {权重类型}“更改为只返回”{权重类型}“,以便在后面添加该人的名称。
  2. 删除了无用的变量weight1hip1weight2hip2.
  3. Changed if to elif所以python只检查一次(可选的更改)
  4. for循环从计算每个for循环中的每个if语句的权重类型中提取出来,而不是检查这个变量是否等于一个变量,然后将值从变量中提取出来,只需在循环中从person变量instead!
  5. Fixed print语句中取出值,以便以所请求的格式打印出来。

G 223

以下是新代码:

代码语言:javascript
运行
复制
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":
        whr = waist / hip
        if whr < 0.8:
            return "normal weight"
        elif 0.8 <= whr <= 0.84:  
            return "overweight"
        else:
            return "obese"
    elif sex == "male":
        whr = waist / hip
        if whr < 0.9:
            return "normal weight"
        elif 0.9 <= whr <= 0.99:     
            return "overweight"
        else:
            return "obese"

for person in people:
    name = person.get("name")
    sex = person.get("sex")
    waist = person.get("waist")
    hip = person.get("hip")
    print(name, "is", waist_to_hip_ratio_2(sex, waist, hip))

享受吧!

票数 0
EN

Stack Overflow用户

发布于 2020-07-05 18:46:37

这个密码起作用了。如果你有任何问题,我很高兴在评论中回答它们。

代码语言:javascript
运行
复制
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:
    name = person["name"]
    sex = person["sex"]
    waist = person["waist"]
    hip = person["hip"]
    print(waist_to_hip_ratio_2(sex, waist, hip).replace("person" , name))

输出:

代码语言:javascript
运行
复制
Christy is normal weight
Edward is obese
Helena is overweight
George is overweight
Sophia is normal weight
票数 0
EN

Stack Overflow用户

发布于 2020-07-05 18:50:15

下面是一些你可以做的事情:

代码语言:javascript
运行
复制
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")

输出:

代码语言:javascript
运行
复制
Christy is normal weight
Edward is obese
Helena is overweight
George is overweight
Sophia is normal weight
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62744885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档