0

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.

Steven Rumbalski
  • 39,949
  • 7
  • 78
  • 111
Chris
  • 11
  • 1
  • 2
  • 2
    Hint: in programming, "remainder" usually goes by a different name: "modulus". This may help you in your research. – Kevin Oct 21 '14 at 14:10

3 Answers3

3

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.
Community
  • 1
  • 1
Cory Kramer
  • 98,167
  • 13
  • 130
  • 181
  • 1
    +1. Since OP is a new programmer, you may wish to explain the difference between true division and floor (integer) division. Or link to some docs. – Steven Rumbalski Oct 21 '14 at 14:16
  • Thank you so much Cyber, I have got it all working now. Hope you don't mind if I ask for your help again in the future if I need too. – Chris Oct 21 '14 at 14:20
1

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)
igon
  • 2,841
  • 1
  • 19
  • 33
  • To everyone who answered, thank you very much. All the help is greatly appreciated. Right now i'm practicing with a textbook my tutor gave me, but it doesn't have any answers so unfortunately when I get stuck I've got no reference to use. So I hope you all don't mind if I end up asking a lot of questions in a small amount of time. – Chris Oct 21 '14 at 14:28
1

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.

jcabrera
  • 126
  • 4