I have worked on a Programm that asks for two certain times (start time and end time) using user input.
It is formatted xx:yy or hours:minutes
I want that the difference between the two given times will be calculated and printed in two forms (properly{xx:yy}, and improperly{xx Hours and yy Minutes}
I tried this for getting the user input:
start1 = int(input("1st Start Time: "))
end1 = int(input("1st EndTime: "))
The problem here was that the colon could not be converted into a number so it couldn't just simply have been subtracted from each other.
And for your knowledge, this Programm ist made for calculating work time from start and end.
So I want the user to input start and end time and get the result, and the same thing ten times and then it will be summed up (all the work times) to create a final work time! There also should be an option to end it any time by typing "done" and get the final sum without having to got trough all ten mini-work-timez.
You can take the user's input and convert to DateTime objects. then simply subtract the two values.
You can store the values in your program and work on the logic to sum those values and handle your "done" requirement.
from datetime import datetime, timedelta
start = 'abcd'
end = '14:31'
try:
s = datetime.strptime(start, "%H:%M")
e = datetime.strptime(end, "%H:%M")
delta = e - s
print(delta)
print(f"{str(delta).split(':')[0]} Hours and {str(delta).split(':')[1]} Minutes")
except Exception as UserInputError:
print(f"Could not parse time from user input. Error: {UserInputError}")
#Properly: 2:01:00
#Improperly: 2 Hours and 01 Minutes
# With Bad input:
# Could not parse time from user input. Error: time data 'abcd' does not match format '%H:%M'