2016-05-24 9 views
0

私は以下の辞書を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? ")} 

は今、私はプログラムは、運動辞書に対するクライアント辞書に入力された値をチェックしてからから/出力ルーチンを返すようにしたいですクライアントが望むものと一致する運動。これはできますか?

+0

は、これらのは、あなたが持っているだろう唯一の演習はありますか? –

+0

私はより多くの練習を潜在的に8つ持っていることを望んでいますが、進歩する前に私が望むことができるかどうか疑問に思っています。 – ScouseCoder

答えて

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"]) 
+0

優れてありがとう、あなたはそれが単純に見えるようにしました。 – ScouseCoder

関連する問題