Reputation: 11
I need help converting this into an hour and minute format using the remainder operator. I'm relatively new to python and coding in general, so help is greatly appreciated.
#Define the value of our variables
numberOfEpisodes = 13
minutesPerEpisode = 42
#Calculate the results
totalMinutes = numberOfEpisodes * minutesPerEpisode
equivalency=totalMinutes//minutesPerHour
#Display the output
print(numberOfEpisodes, 'episodes will take', totalMinutes, 'minutes to watch.') print('This is equivalent to', equivalency)
This is what I currently have, I am able to obtain the amount of hours there are, but I can't figure out how to adjust the code to include the remaining minutes.
Sorry if I don't make a whole lot of sense, but hopefully you'll understand.
Upvotes: 0
Views: 3467
Reputation: 117971
You can use //
integer division and %
modulus for remainder. (You can read more about Python's int
and float
division here)
>>> numberOfEpisodes = 13
>>> minutesPerEpisode = 42
>>> totalMinutes = numberOfEpisodes * minutesPerEpisode
>>> totalMinutes
546
>>> minutesPerHour = 60
>>> totalHours = totalMinutes // minutesPerHour
>>> totalHours
9
>>> remainingMinutes = totalMinutes % minutesPerHour
>>> remainingMinutes
6
Result
>>> print('{} episodes will take {}h {}m to watch.'.format(numberOfEpisodes,totalHours, remainingMinutes))
13 episodes will take 9h 6m to watch.
Upvotes: 3
Reputation: 146
Check out the timedelta documentation in the datetime module documents. You can create the durations as time deltas in minutes, and then when you want to display it you can ask timedelta to give it in whatever format you want. You could use arithmetic calculations to get the hours and minutes, but if you then need to use this data to calculate dates and times, for example to know at what time the show will be over if it starts at 09:00, you will have many extra steps to go through, instead of just using the timedelta.
Upvotes: 1
Reputation: 3046
Use the modulo operator %
#Define the value of our variables
numberOfEpisodes = 13
minutesPerEpisode = 42
#Calculate the results
totalMinutes = numberOfEpisodes * minutesPerEpisode
equivalency=totalMinutes//60
minutes= totalMinutes%60
#Display the output
print(numberOfEpisodes, 'episodes will take', totalMinutes, 'minutes to watch.')
print('This is equivalent to', equivalency,minutes)
Upvotes: 1