jeudi 7 décembre 2017

append item to list at a random index os initial list

i have a list of answers to a quiz i a making, i would like to make it multiple choice and in multiple choice quizes the answer is never at the bottom always or at the same index however this is what my code is

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

this returns eg for one iteration: what is the capital of: colombia? london carracas brasilia santiago bogota

notice that bogota is at the bottom due to the .append(Ca[q.insert(x)])

what i would like is that the answer in this case would be inserted into Ca(correct answers) randomly. is there a way to do this?

  • Answers means the genral list of all the answers possible Questions means the general list of all questions possible

    • in both of the above lists each element is reference by its index so
      that by finding the index of the element in Questions it is possible to find the value held in Answers by the same index

    q means questions selected randomly for quiz Ca means correct answers for questions in q. Pa means possible answers, randomly obtained from the genral array Answers.




Aucun commentaire:

Enregistrer un commentaire