2017-12-07 5 views
-2

私はクイズ作成の回答リストを持っています。私はそれを複数の選択肢にしたいと考えています。 の首都です::コロンビアが同じインデックスは、しかし、これは私のコードは、これは一回の反復のために例えばを返しランダムなインデックスでリストに項目を追加するosの初期リスト

Answers = ["bogota", "carracas", "brasilia", "santiago", "london"] 
Questions = ["colombia", "venezuela", "brasil", "chile", "england"] 
q = [Questions[i] for i in sorted(random.sample(range(len(Questions)), 3))] 
tryindex = [i for i, x in enumerate(QuestionsT) if x in q] 
Ca = [Answers[i] for i in tryindex] 
for x in q: 
    Pa = [i for i in random.sample(Answers, 3) if i !=q.index(x)] 
    Pa.append(Ca[q.index(x)]) 
    print("what is the capital of:" + x + "?") 
    print("\n".join(Pa)) 
    a = input("\n""Answer") 
    for i in range(0,3): 
     if a == Ca[i]: 
      score +=1 

は何かありますか? ロンドンcarracas ブラジリア サンティアゴボゴタ起因.appendに下にあること

通知(CA [q.insert(X)])

私が希望することである回答でこのケースはCa(正解)にランダムに挿入されます。これを行う方法はありますか?

  • 回答がすべての答えのgenralリストに可能な質問を意味 上記のリストの両方で

    • 可能すべての質問の一般的なリストを意味し、各要素は、そのインデックスによって参照されるように
      こと質問の要素のインデックスを見つけることによって、同じインデックスによって回答に保持されている値を見つけることが可能なのは です。

    qは、クイズのためにランダムに選択された質問を意味します。 Caは、qの質問に対する正解を意味します。 Paは、genral配列Answersから無作為に得られる可能な回答を意味します。

+0

インデックスを使用しないでください。圧縮したqとaを一緒にランダム化するか、dict質問=>答えを作成します。 –

+0

あなたの質問&回答リストには引用符がありません。コードは動作しません –

答えて

0

ここはクリーンな解決策です。見てみましょう。あなたが望むものを変更して、得られないものを求めてください。

# -*- coding: utf-8 -*- 

# Imports 
import random 

# Parameters 
data = {'Brasil': 'Brasilia', 
'Chile': 'Santiago', 
'Colombia': 'Bogota', 
'England': 'London', 
'Venezuela': 'Carracas'} 

nbr_questions = 3 
score = 0 
former_questions = [''] 

# Script 
for i in range(nbr_questions): 
    # Grab the couple country/capital from the data dictionnary 
    capital = '' 
    while capital in former_questions: 
     country, capital = random.choice(list(data.items())) 

    # Create the proposition display list 
    proposition_display = list() 
    proposition_display.append(capital) 
    i = 0 
    while i < 2: 
     cap = random.choice(list(data.values())) 
     if cap not in proposition_display: 
      proposition_display.append(cap) 
      i += 1 

    # Display 
    print ('What is the capital of {} ?'.format(country)) 
    answer = input ('Answer: ') 

    if answer.lower().strip(' ') == capital.lower().strip(' '): 
     print ('Correct!') 
     score += 1 
    else: 
     print ('Wrong. Answer was {}.'.format(capital)) 

    print ('-----------------------------') 

    # Add this capital to the list former_questions to avoid repetition 
    former_questions.append(capital) 

print ('Your score is: {}'.format(score)) 
関連する問題