Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I create a code that charges a 'penny' for every second that my code is ran? (Python)

I want to create a code that charges ‘money’ for every second that my program is ran. For example, if I am charging 1 cent for every second that the program is ran, and the program is ran for 10 seconds, I want a statement to be printed like "You have ran the program for 10 seconds, and will be charged 10 cents" at the end of my program.

I don’t even know where to start on this, but so far, I’ve created a code that shows me how long my program has been ran for. The code looks like this:

from datetime import datetime
start_time = datetime.now()
#my code
end_time = datetime.now()
print("Duration:", end_time-start_time)

How can I go from here?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You’re mostly there; just get the total_seconds() from the timedelta you got from subtracting the two datetimes.

elapsed = (end_time-start_time).total_seconds()
print(f"You have ran the program for {elapsed} seconds, and will be charged {elapsed} cents.")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading