user1938322
user1938322

Reputation: 21

random order python

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

Upvotes: 1

Views: 7211

Answers (1)

Abhijit
Abhijit

Reputation: 63727

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']

Upvotes: 9

Related Questions