1

I have to make a quiz for a project. I have the question and 3 answers but the correct one always come on the top. How do I shuffle my answers?

QS = []
for row in data:
         row.append(2)
         QS.append(row)

while True:
    x=random.randint(0,len(QS)-1)
    if QS[x][2]>0:
        print QS[x][0]
        break


while True:
    if QS[x][2]>0:
        print QS[x][1]
        break

while True:
    xy=random.randint(0,len(QS)-1)
    if xy!=x:
        print QS[xy][1]
        break
while True:
    xyz=random.randint(0,len(QS)-1)
    if xyz!=xy and xyz!=xy:
        print QS[xyz][1]
        break

1 Answer 1

9

Use random.shuffle. First create a list of valid answer and use random.shuffle to create a random shuffle

An example demonstration

>>> import random
>>> ans = ['ans1', 'ans2', 'ans3']
>>> random.shuffle(ans)
>>> ans
['ans1', 'ans3', 'ans2']
Sign up to request clarification or add additional context in comments.

1 Comment

could you try to do it with my code if possible if needed i could send you the information needed it would be really appreciated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.