lundi 7 décembre 2015

python 3 random picking of statement from given file

hi there friend am new to python and was trying to write a program that randomly picks the questions with options from a file and displays it when a user enters the answer it should jump to next question as i mentioned i want the question to be randomly picked i have tried using "random" function but was unable to do so below is given program.

    import random
a=open('1\q2.txt')
line = a.readlines()
lineq = line[1].split('$')
lineq1 = line[2].split('$')

total = random.sample (print1, print2)
print1 = print(lineq[1:5])

user_input = input()


if user_input == lineq[-1]:
    print('your answer is right')
else:
    print('your answer is wrong the answer is :',lineq[-1])

print2 = print(lineq1[1:5])

user_input = input()

if user_input == lineq1[-1]:
    print('your answer is right')
else:
    print('your answer is wrong the answer is :',lineq1[-1])


a.close()

And the content in the q2.txt that is the question file is below

1$Which is the largest island in the world?$Srilanka$Australia$Greenland$Greenland 2$Which country is known as the lady of snow?$Greenland$Canada$Pakistan$Canada 3$In which year was the first electronic computer made?$1937$2000$2005$1937 4$Which of these have no external ears?$Giraffe$frog$cat$frog 5$Which of the following is a carnivore?$Vulture$crow$goat$Vulture

Kindly please help me with this.




Aucun commentaire:

Enregistrer un commentaire