python - 如何在函数中适当地使用字典中的值
问题描述
我正在编写一段代码,但我似乎无法弄清楚如何在函数中使用字典中的项目。我希望输出以这种方式给出几行:
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))
解决方案
您可以执行以下操作:
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
推荐阅读
- azure-data-catalog - 如何通过 REST API 为 Azure 数据目录资产设置友好名称?
- regex - Apache 重写,包括查询字符串操作
- amazon-web-services - 我们可以在无服务器架构上部署 QlikSense 吗?
- asp.net - 如何解决 ASP.NET MVC 和 sql server 2019 中系统找不到文件指定错误
- java - 从 Java Spring Boot 中的 Principal 中提取数据
- lua - Lua 表被覆盖
- java - 使用真实设备在颤振中调试第一个应用程序时出错
- c++ - TBB 并行管道:过滤器时序不一致
- python - 通过 XML 文件和 netsh wlan(或 Python)更改 wifi 配置文件的密码
- python - 平台问题中无法加载级别列表