Skip to content Skip to sidebar Skip to footer

TypeError: Unsupported Operand Type(s) For -: 'datetime.datetime' And 'int'

I'm trying to build a chronometer but I'm having trouble calculating the delta_time `def start():` start_time = datetime.datetime.now() print(start_time) def stop(): s

Solution 1:

The error you are showing doesn't indicate that the issue is with variable scope, rather it is encountered based on calculations not being able to be done on non-like types.

It's difficult to say without seeing your full code, but perhaps the following will provide a possible alternative solution:

#!/usr/bin/env python

import datetime

def start():
    start_time = datetime.datetime.now()
    return start_time

def stop():
    stop_time = datetime.datetime.now()
    return stop_time

def delta(start,stop):
    delta_time = stop - start
    print(delta_time)

start=start()
stop=stop()
delta(start,stop)

This solution moves the two generated timestamps (start_time and stop_time) into one single function (delta) as arguments so that when delta_time is calculated it is done on like-type variables within its function scope.


Post a Comment for "TypeError: Unsupported Operand Type(s) For -: 'datetime.datetime' And 'int'"