Quiz

By sunslayer on Feb 02, 2011

started to learn python yesterday and found http://www.daniweb.com/forums/post161212-13.html so i figured I'd give it a try

basic quiz, randomizes the questions and keeps track of right/wrong answers and tells you your score at the end

quiz file:http://www.daniweb.com/forums/attachment.php?attachmentid=1388&d=1126839638

after you download the file edit the last line of the code to the correct path

from re import findall
from random import randint
class Quiz:
    w = r = t = 0
    def __init__(self, f):
        h = open(f)
        self.q = []
        for a in findall("([A-D])\n(.+)\n(.+)\n(.+)\n(.+)\n(.+)\n?",h.read()):
            self.q.append(a)
        self.shuffle()
        h.close()
    def shuffle(self):
        s = []
        for x in self.q:
            s.insert(randint(0,len(s)), x)
        self.q = s
    def start(self):
        for q in self.q:
            rans = q[0].upper()
            b = 1
            print "Question", str(self.t+1) + ":", q[1]
            for x in "ABCD":
                b += 1
                print x + ".", q[b]
            ans = raw_input("Answer: ")
            if ans.upper() != rans: self.w += 1
            else: self.r += 1
            self.t += 1
            print ""
        print "End of Quiz!"
        print "you answered", str(self.r), "out of", str(self.t), "questions right! (" + str(round((float(self.r)/float(self.t))*100,2)) + "%)"
        pa = raw_input("Play again(y/n)? ")
        if pa.lower() == "y":
            self.w = self.r = self.t = 0
            self.shuffle()
            self.start()

quiz = Quiz("PATH\TO\QUIZ").start()

Comments

Sign in to comment.
sunslayer   -  Feb 12, 2011

from command line, cd to your python dir and type
python PATH_TO_FILE.py

 Respond  
nescient   -  Feb 12, 2011

Sorry for asking that.. I'm a real dumbass (or so it seems).. but how do you start the quiz?

 Respond  
sunslayer   -  Feb 02, 2011

thanks, :)

 Respond  
Sorasyn   -  Feb 02, 2011

Damn good for just starting yesterday. :)

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.