我正在编写一段代码,我似乎不知道如何在函数中使用字典中的条目。我希望输出以这种方式给出几行:
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))发布于 2020-07-05 18:56:09
下面是我对您的代码所做的更改:
weight1、hip1、weight2、hip2.if to elif所以python只检查一次(可选的更改)for循环从计算每个for循环中的每个if语句的权重类型中提取出来,而不是检查这个变量是否等于一个变量,然后将值从变量中提取出来,只需在循环中从person变量instead!print语句中取出值,以便以所请求的格式打印出来。G 223
以下是新代码:
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))享受吧!
发布于 2020-07-05 18:46:37
这个密码起作用了。如果你有任何问题,我很高兴在评论中回答它们。
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))输出:
Christy is normal weight
Edward is obese
Helena is overweight
George is overweight
Sophia is normal weight发布于 2020-07-05 18:50:15
下面是一些你可以做的事情:
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 weighthttps://stackoverflow.com/questions/62744885
复制相似问题