0

I am creating a program in Python in which the user has to type the alphabet as quick as possible and then the computer outputs the time they took. My code so far is:

import sys
from datetime import *

ready = raw_input('Press enter when ready')

first = datetime.now().time()
alph = raw_input('TYPE!!!')
second = datetime.now().time()
if alph != 'abcdefghijklmnopqrstuvwxyz':
    print 'Inocrrect!'
    sys.exit()
else:
    time = second - first
    print 'It took you', time.seconds

The programs has an error when working out the difference between the two times:

TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.time'

How can I fix this?

argentage
  • 2,758
  • 1
  • 19
  • 28
T. Green
  • 323
  • 10
  • 20
  • don't use local time to find the elapsed time, use either UTC time or aware datetime objects. See [Find if 24 hrs have passed between datetimes - Python](http://stackoverflow.com/q/26313520/4279) – jfs Oct 13 '15 at 08:55

2 Answers2

2

You cannot subtract datetime.time from datetime.time objects. It would be better to use datetime.now() (which contains both date and time components). Example -

first = datetime.now()
alph = raw_input('TYPE!!!')
second = datetime.now()
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
1

Arithmetic is not supported on Python's time type. Try using just the datetime instead:

first = datetime.now()
alph = raw_input('TYPE!!!')
second = datetime.now()
Will Vousden
  • 32,488
  • 9
  • 84
  • 95