我在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? ")}
现在我希望程序根据练习词典检查客户端词典中输入的值,然后从与客户想要的练习相匹配的练习中返回/输出例程。可以这样做吗?
答案 0 :(得分:0)
我会将每个练习字典放在一个列表中并遍历列表:
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"])