Timing a quiz question

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 2866 Views - Last Post: 29 September 2012 - 03:14 PM Rate Topic: -----

#1 Osbornezo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-September 12

Timing a quiz question

Posted 29 September 2012 - 12:00 AM

ey everyone,

So i have written up a 10 question multichoice quiz. As an extra i thought i may try add a function that times how long the user takes to complete the quiz and displays the total time at the end. I think its this at the start: start = time.clock() then at the end:duration = time.clock() - start.

Here is the start and finish of my code:

print "Hi there, welcome to Mason's who wants to be a millionaire New Zealand Edition"      
time.sleep(3)
def main():

    name=raw_input( "Please type in your name then hit Enter")                              
    
    print '\nHello there', name, 'I hope you have fun. Enjoy and good luck!!'              

    print '\nReady'
    time.sleep(1)
    print '\n3'
    time.sleep(1)
    print '\n2'
    time.sleep(1)
    print '\n1'
    time.sleep(1)
    print'\nGo!'
    

    Question1()
    time.sleep(2)
    Question2()
    time.sleep(2)
    Question3()
    time.sleep(2)
    Question4()
    time.sleep(2)
    Question5()
    time.sleep(2)
    Question6()
    time.sleep(2)
    Question7()
    time.sleep(2)
    Question8()
    time.sleep(2)
    Question9()
    time.sleep(2)
    Question10()
start = time.clock()


    

    


    
def Question1():

    print '\nWhat is the capital of New Zealand?'
    print '\nA) Invercargill'
    print '\nB) Wellington'
    print '\nC) Dunedin'
    print '\nD) Auckland'
    ans1=raw_input ('Answer: ')
    if ans1 == 'A' or ans1=='a':
        print 'Wrong!'
    elif ans1 == 'B'or ans1=='b':
        print 'Correct! Wellington is the Capital of New Zealand, Nice work!'
    elif ans1 == 'C'or ans1== 'c':
        print 'Wrong!'
    elif ans1 == 'D'or ans1== 'd':
        print 'Wrong!'


and the end looks like this

def Question10():

    print'\nName the body of water that separates the North and South Islands'
    print '\nA)Bass Strait'
    print '\nB)Tasman Sea'
    print '\nC)Foveaux Strait'
    print '\nD)Cook Strait'
    ans10=raw_input ('Answer: ')
    if ans10 == 'A' or ans10=='a':
        print 'Wrong!'
    elif ans10 == 'B'or ans10=='b':
        print 'Wrong!'
    elif ans10 == 'C'or ans10== 'c':
        print 'Wrong!'
    elif ans10 == 'D'or ans10== 'd':
        print 'Correct! The body of water that separates the North and South Islands is the Cook Strait!'
        duration = time.clock() - start

    


    

    


        
main()


But i dont get how to display it :( Any help? i though it would just be print but its not :(

Thanks in advance :)

Is This A Good Question/Topic? 0
  • +

Replies To: Timing a quiz question

#2 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Timing a quiz question

Posted 29 September 2012 - 12:09 AM

When you go to print it what does it display? Nothing? Try changing it to time.time() instead of time.clock() and that should work too, and you can definitely print it in my experience.
Was This Post Helpful? 0
  • +
  • -

#3 Osbornezo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-September 12

Re: Timing a quiz question

Posted 29 September 2012 - 02:04 AM

View PostSimown, on 29 September 2012 - 12:09 AM, said:

When you go to print it what does it display? Nothing? Try changing it to time.time() instead of time.clock() and that should work too, and you can definitely print it in my experience.
Its saying that duration is not defined
Was This Post Helpful? 0
  • +
  • -

#4 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Timing a quiz question

Posted 29 September 2012 - 02:19 AM

A problem with your code then. The thing is that you start the timer AFTER question 10 and count the duration only after question 10 is answered correctly, surely that's wrong?

I think it should be:
start = time.time()
Question1()
time.sleep(2)
...
Question10()
end = start - time.time()


Was This Post Helpful? 0
  • +
  • -

#5 Osbornezo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-September 12

Re: Timing a quiz question

Posted 29 September 2012 - 03:01 AM

View PostSimown, on 29 September 2012 - 02:19 AM, said:

A problem with your code then. The thing is that you start the timer AFTER question 10 and count the duration only after question 10 is answered correctly, surely that's wrong?

I think it should be:
start = time.time()
Question1()
time.sleep(2)
...
Question10()
end = start - time.time()

Now it says end is not defined, do i have to do something like def timer() ?
Was This Post Helpful? 0
  • +
  • -

#6 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Timing a quiz question

Posted 29 September 2012 - 03:31 AM

"end" is a variable name. It's defined when you type it. No, you don't need a timer function.

It may not be in scope when you are trying to print it though. Can you post the code where you attempt to print the time?

This post has been edited by Simown: 29 September 2012 - 03:32 AM

Was This Post Helpful? 0
  • +
  • -

#7 Osbornezo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-September 12

Re: Timing a quiz question

Posted 29 September 2012 - 03:39 AM

View PostSimown, on 29 September 2012 - 03:31 AM, said:

"end" is a variable name. It's defined when you type it. No, you don't need a timer function.

It may not be in scope when you are trying to print it though. Can you post the code where you attempt to print the time?
def Question10():

    print'\nName the body of water that separates the North and South Islands'
    print '\nA)Bass Strait'
    print '\nB)Tasman Sea'
    print '\nC)Foveaux Strait'
    print '\nD)Cook Strait'
    ans10=raw_input ('Answer: ')
    if ans10 == 'A' or ans10=='a':
        print 'Wrong!'
    elif ans10 == 'B'or ans10=='b':
        print 'Wrong!'
    elif ans10 == 'C'or ans10== 'c':
        print 'Wrong!'
    elif ans10 == 'D'or ans10== 'd':
        print 'Correct! The body of water that separates the North and South Islands is the Cook Strait!'
    print end == start - time.time()

Was This Post Helpful? 0
  • +
  • -

#8 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Timing a quiz question

Posted 29 September 2012 - 03:44 AM

Just one equals:
print end = start - time.time()

You are doing a comparison to end which doesn't exist (yet)
Was This Post Helpful? 0
  • +
  • -

#9 Osbornezo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-September 12

Re: Timing a quiz question

Posted 29 September 2012 - 03:53 AM

View PostSimown, on 29 September 2012 - 03:44 AM, said:

Just one equals:
print end = start - time.time()

You are doing a comparison to end which doesn't exist (yet)
With one = i get invalid syntax, thats why i put two in
Was This Post Helpful? 0
  • +
  • -

#10 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Timing a quiz question

Posted 29 September 2012 - 03:57 AM

Ok, assign then print - and it's definitely defined

end = start - time.time()
print end


This post has been edited by Simown: 29 September 2012 - 04:15 AM

Was This Post Helpful? 0
  • +
  • -

#11 Osbornezo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-September 12

Re: Timing a quiz question

Posted 29 September 2012 - 04:02 AM

View PostSimown, on 29 September 2012 - 03:57 AM, said:

Ok, assign then print - and it's definitely defined

end = start = time.time()
print end

It worked! Thank you! but the time appeared to be very precise lol, something like this 1352463.7, anyway to get it into seconds?
Was This Post Helpful? 0
  • +
  • -

#12 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Timing a quiz question

Posted 29 September 2012 - 04:07 AM

As far as I know - it is in seconds. To less decimal places perhaps?
Was This Post Helpful? 0
  • +
  • -

#13 Osbornezo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-September 12

Re: Timing a quiz question

Posted 29 September 2012 - 04:09 AM

View PostSimown, on 29 September 2012 - 04:07 AM, said:

As far as I know - it is in seconds. To less decimal places perhaps?
Yes anything but what its showing, i dont have to have this in my program but im trying to learn as many different things to make my programs more interesting and in depth
Was This Post Helpful? 0
  • +
  • -

#14 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Timing a quiz question

Posted 29 September 2012 - 04:15 AM

Ok, to round it to 2 decimal places, which people are probably more used to use:

end = start - time.time()
end = round(end, 2)
print end



And it should come out looking nicer

This post has been edited by Simown: 29 September 2012 - 04:19 AM

Was This Post Helpful? 0
  • +
  • -

#15 Osbornezo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-September 12

Re: Timing a quiz question

Posted 29 September 2012 - 04:28 AM

View PostSimown, on 29 September 2012 - 04:15 AM, said:

Ok, to round it to 2 decimal places, which people are probably more used to use:

end = start - time.time()
end = round(end, 2)
print end



And it should come out looking nicer
Thank you, if i were to put a message in there saying you took (however many seconds) to complete this quiz how would i go about it? i was thinking something like this
end = round(start - time.time(), 2)
        print end = 'you complete this quiz in (number) amount of seconds 
Not too sure on how do add that bit
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2