2017-06-28 11 views
0

私は現在、クイズに取り組んでいます。誰もが私が受け続けるエラーで私を助けることができるかどうか私はさまよっていた。私はそれがランダムな順序で質問をし、それらを他の質問の答えと質問を与えて保持しないようにそれを作るしようとしているリストを別のリストでランダム化する

num1 = num = 0 
import random 

questions = ["What is RAM?","what is ROM","What is a bundle of wires 
carrying data from one component to another?","What does the control unit 
do?"] 

ans = [["Random access memory","real access memory","read access 
memory","readable access memory","A"],["Readable object memory","Random 
object memory","Read only memory","Read object memory","C"], 
["Bus","Hardware","System software","Embedded systems","A"],["You type on 
it","It sends out control signals to other components","It calculates 
arithmetic problems","Regulates time and speed of computer functions","D"]] 

for index in range(0, len(questions)): 
    val = [0, 1, 2, 3] 
    index = random.choice(val) 
    print(questions[index]) 
    print("\nA:",ans[index][0],"\nB:",ans[index][1],"\nC:",ans[index] 
     [2],"\nD:",ans[index][3],"")  
    pa = input("What is your answer?") 
    if pa == ans[index][4]: 
     num1, num = num1 + 1, num + 1 
     print("Correct!\nYou have got",num1,"out of",num,"correct so far\n") 
     questions.remove(questions[index]) 
     ans.remove(ans[index]) 
     val.remove(index) 
    else: 
     num = num + 1 
     print("incorrect!\nThe correct answer was",ans[index][4],"Your 
     correct questions are Your incorrect questions are") 

:ここに私のコードです。誰でもそれがどうやって行えるか知っていますか? 助けていただければ幸いです。インデックスの

答えて

0

で質問を選びます。タプルのリスト

questions = [('What is RAM?', 'random access memory'), ('What is ROM?', 'read-only memory')] 

その後

一緒に入れて
question = random.choice(questions) 

を実行して、質問を選び出すために、それはあなたの元のスクリプトにかなり近い見てみましょう:

import random 

questions = [('What is RAM?', 'random access memory'), ('What is ROM?', 'read-only memory')] 

while questions: 
    question = random.choice(questions) 

    print(question[0]) 
    user_response = input('Reponse> ') 
    if user_response == question[1]: 
     print('Correct!') 
     questions.remove(question) 
    else: 
     print('Wrong!') 

demo

+0

はい、それは 'questions.remove(question)'を読むべきです – Will

+0

ありがとう!:) – johnnu

1

使用random.shuffle:次に

>>> sequence = list(range(len(questions))) 
>>> random.shuffle(sequence) 
>>> sequence 
[2, 0, 3, 1, ....] 

私は1つのリストに質問と回答を統合するお勧めします

for index in sequence: 
    question = questions[index] 
+0

[OK]を私がしますこれを今お試しください。 – johnnu

+0

範囲でエラーが発生しました: "TypeError: 'range'オブジェクトが項目割り当てをサポートしていません" – johnnu

+1

@ johnnu Python 3で 'sequence = list(range(len(questions))'を使用する必要があります –

関連する問題