2017-10-05 26 views
1

私はこのPythonコードを書いています。ユーザーが複数の選択肢を持つ簡単で難しいモードを選択できるようになります。各モードの質問は同じですが、ハードバージョンには各質問から選択するオプションが増えています。これは私のコードは、これまでのところです:クイズPythonで簡単で簡単なモードを作成する

questions = ["What is 1 + 1", 
     "What is Batman's real name"] 
answer_choices = ["1)1\n2)2\n3)3\n4)4\n5)5\n:", 
       "1)Peter Parker\n2)Tony Stark\n3)Bruce Wayne\n4)Thomas Wayne\n5)Clark Kent\n:"] 
correct_choices = ["2", 
       "3",] 
answers = ["1 + 1 is 2", 
     "Bruce Wayne is Batman"] 

def quiz(): 
    score = 0 
    for question, choices, correct_choice, answer in zip(questions,answer_choices, correct_choices, answers): 
     print(question) 
     user_answer = str(input(choices)) 
     if user_answer in correct_choice: 
      print("Correct") 
      score += 1 
     else: 
      print("Incorrect", answer) 
    print(score, "out of", len(questions), "that is", float(score /len(questions)) * 100, "%") 

quiz() 

は、どうすればより多くの新しいリストを作成し、すべてをコピー&ペーストすることなく、簡単かつハードを追加するのでしょうか?説明もいいだろう。あなたは、機能ブロックを定義するなど、ユーザーの入力何かに基礎それらを呼び出すことができます

+0

簡易モードリストといくつかの追加情報(追加オプション)に基づいてハードモードリストを生成するコードを記述する必要があります。これを行う最善の方法は、主に意見の問題です。 –

+0

辞書を調べる必要があります。 – Chris

+0

質問がどのように提起されたかを考えれば、「Bruce Wayne is Batman is Batman is Batman is」と言い換えることを考慮する必要があります:) –

答えて

2

すべての質問のリストを作成し、必要に応じて難易度に基づいてスプライスすることができます。

def get_choices(difficulty): 
    choices = [ 
     "1)1\n2)2\n3)3\n4)4\n5)5\n:", 
     "1)Peter Parker\n2)Tony Stark\n3)Bruce Wayne\n4)Thomas Wayne\n5)Clark Kent\n:" 
    ] 

    if difficulty == 'easy': 
     choices = [c.split("\n")[:3] for c in choices] 
     return choices 
    elif difficulty == 'medium': 
     choices = [c.split("\n")[:4] for c in choices] 
     return choices 
    else: 
     return choices 

リスト要素を個別に選択し、解決策を対応させることができれば、より簡単になります。その後、正しい解決策を得て、他の回答をシャッフルして自動的に数値を割り当てることができます。

+0

正常に動作しますが、新しい行にどのように各要素を出力しますか?選択肢の各要素は、印刷時に改行されます。私はそこから残りを行うことができると思います – C4RN4GE

+0

この 'user_answer = str(input(choices))'を変更すると、すべての選択肢を繰り返し出力し、 'user_answer = input()'や ''\ nその行を保持したい場合は '.join(choices) 'を選択します。 – 4d11

0

任意の回答を事前に ありがとう:

# define the function blocks 
def hard(): 
    print ("Hard mode code goes here.\n") 

def medium(): 
    print ("medium mode code goes here\n") 

def easy(): 
    print ("easy mode code goes here\n") 

def lazy(): 
    print ("i don't want to play\n") 

# Now map the function to user input 
choose_mode = {0 : hard, 
      1 : medium, 
      4 : lazy, 
      9 : easy, 

} 
user_input=int(input("which mode do you want to choose : \n press 0 for hard \n press 1 for medium \n press 4 for lazy \n press 9 for easy ")) 
choose_mode[user_input]() 

次に機能ブロックの呼び出しは次のようになります。

choose_mode[num]() 
0

ワンそのようなものを実装する方法は、各質問について、正解と可能性のある誤答を含むリストから始めることです。

次に、各質問に対して、難易度に応じて正しい間違った質問を選んで、その基本リストから実際の質問リストを生成するコードを作成します。生成されたその新しいリストは、質問をするために使用されます。

関連する問題