简体   繁体   中英

How to divide by time Python

I want my program to calculate how many jobs it completes per minute.

Here is what the program already knows:

ORIGINAL_NUM_OF_TOTAL_JOBS = this is the number of jobs it was told to complete NUM_OF_TOTAL_JOBS_REMAINING = This is the number of jobs remaining

 print "\nTime Elapsed:" 
            TIME_ELAPSED = datetime.now() - STARTTIME
            print TIME_ELAPSED
            JOBS_PER_MINUTE = ORIGINAL_NUM_OF_TOTAL_JOBS - NUM_OF_TOTAL_JOB_REMAINING / TIME_ELAPSED
            print "\nAttempts/minute: "
            print JOBS_PER_MINUTE

I know the logic isn't right above because I'm dividing by "time" and time doesnt neccessarily = per minute, but im stuck.

Output/Error Message from python

Time Elapsed:
0:00:00.146000
Traceback (most recent call last):
  File "C:\Users\jlemming\Documents\NetBeansProjects\myscript.py\src\myscript.py", line 117, in <module>
    ATTEMPTS_PER_MINUTE = ORIGINAL_NUM_OF_TOTAL_ATTEMPTS - NUM_OF_TOTAL_ATTEMPTS / TIME_ELAPSED
TypeError: unsupported operand type(s) for /: 'int' and 'datetime.timedelta'

TIME_ELAPSED is a datetime.timedelta object ( https://docs.python.org/2/library/datetime.html#datetime.timedelta ). It has the method total_seconds() . Divide that by 60 to get the time elapsed in minutes.

TIME_ELAPSED = datetime.now() - STARTTIME
minutes = TIME_ELAPSED.total_seconds() / 60.0
ATTEMPTS_PER_MINUTE = (ORIGINAL_NUM_OF_TOTAL_ATTEMPTS - NUM_OF_TOTAL_ATTEMPTS) / minutes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM