在 Python 中比较字典中的值并返回一个值



我在python中设置了以下字典。

Ex1={"Upper_body" : True, "Lower_body" : False, "Core" : False,'routine': "4 x 8 reps arm curl, 4 x 8 reps chest press, 4 x 8 reps shoulder press"}
Ex2={"Upper_body" : True, "Lower_body" : False, "Core" : False,'routine':"4 x 8 reps squats, 4 x 8 reps leg press, 20 x lunges"}
Ex3={"Upper_body" : True, "Lower_body" : False, "Core" : True,'routine':"4 x 8 reps arm curl, 4 x 8 reps chest press, 4 x 8 reps shoulder press, 20 min plank"}

我想让客户选择他们想要专注于身体的哪些部位。 所以我设置了以下代码来找出客户端想要什么:

def yes_no(question):
answer = input(question).lower()
if answer=='yes':
    ans=True
else:
    ans=False
return(ans)
client = { "Upper_body"  : yes_no("Do you want to exercise the upper body? "),
     "Lower_body" : yes_no("Do you want to exercise the lower body? "),
     "Core" : yes_no("Do you want to exercise the core muscle group? ")}

现在,我希望程序根据练习字典检查在客户端字典中输入的值,然后从练习中返回/输出与客户端所需匹配的例程。 这能做到吗?

我会将每个练习字典放在一个列表中并迭代列表:

exercises = [
    {"Upper_body" : True, "Lower_body" : False, "Core" : False,'routine': "4 x 8 reps arm curl, 4 x 8 reps chest press, 4 x 8 reps shoulder press"},
    {"Upper_body" : True, "Lower_body" : False, "Core" : False,'routine':"4 x 8 reps squats, 4 x 8 reps leg press, 20 x lunges"},
    {"Upper_body" : True, "Lower_body" : False, "Core" : True,'routine':"4 x 8 reps arm curl, 4 x 8 reps chest press, 4 x 8 reps shoulder press, 20 min plank"}
             ]
def yes_no(question):
    answer = input(question).lower()
    if answer=='yes':
        ans=True
    else:
        ans=False
    return(ans)

client = { "Upper_body"  : yes_no("Do you want to exercise the upper body? "),
     "Lower_body" : yes_no("Do you want to exercise the lower body? "),
     "Core" : yes_no("Do you want to exercise the core muscle group? ")}

for exercise in exercises:
    if exercise["Upper_body"] == client["Upper_body"] and exercise["Lower_body"] == client["Lower_body"] and exercise["Core"] == client["Core"]:
        print(exercise["routine"])

相关内容

  • 没有找到相关文章

最新更新