0

My question is how I'd be able to add the minutes and calculate them into hours and minutes.

Here some code that I have:

import math

def main():   

    total = 0
    for i in range (number):
        task = int(input('How many minutes will this task take? '))
        print(task)
        total += task
    print(total)
    hour = total // 60
    minute = hour % 0
    print('Hour(s): ', hour, 'Minute(s): ', minute)

main()
  • 1
    Do you know how to convert from a number representing minutes, to two numbers representing hours and minutes? On paper for example? Say you have the number 146, how many hours are in there, and ho many minutes remaining? – Martijn Pieters Feb 06 '15 at 07:38
  • @MartijnPieters 146 minutes would be 2 hours and 26 minutes, I believe. –  Feb 06 '15 at 07:39
  • 1
    So how would you go about calculating that if the number was instead written down as `x`? – Martijn Pieters Feb 06 '15 at 07:43
  • @MartijnPieters So `x = 146`? Then I guess 146 divided by 60 which would be 2 hours and then the minutes would be the remainder; 26 minutes –  Feb 06 '15 at 07:48
  • Do you think you can code that up in Python? There are [several arithmetic operators](https://docs.python.org/3/reference/expressions.html#binary-arithmetic-operations) you could use here. Division and remainder are both among them, if you read the documentation carefully you may even find **one** function that can do the job here. – Martijn Pieters Feb 06 '15 at 07:49
  • A gentler overview could be the [*Using Python as a Calculator* section of the tutorial](https://docs.python.org/3/tutorial/introduction.html#using-python-as-a-calculator). – Martijn Pieters Feb 06 '15 at 07:51
  • @MartijnPieters I'll try but the program asks how many tasks you want to do and then I think it should add those together. I'm pretty sure I know what to do afterwards. –  Feb 06 '15 at 07:52
  • You already have a loop working, and you already have numeric input working. I gave you the tools to do arithmetic, so why not try and see if you can add up the numbers in that loop you already have? – Martijn Pieters Feb 06 '15 at 07:53
  • The reason I focused on the method to go from minutes to hours and minutes is because that is basically the only part you appeared to be missing here. – Martijn Pieters Feb 06 '15 at 07:54
  • @MartijnPieters I'll go ahead and try. I'll get back to you on it. –  Feb 06 '15 at 07:54
  • Do you want to print the total duration of all the tasks, or do you want to print the clock time that the last task finishes? – PM 2Ring Feb 06 '15 at 07:54
  • @MartijnPieters I got it to either print minutes or hours not both or add them together. For instance I would type in 2 tasks. Then 30 minutes then 40 minutes. The output would be: 'Hour(s): 0 Minute(s): 40' instead of 'Hour(s): 1 Minute(s): 10' –  Feb 06 '15 at 07:57
  • total duration @PM2Ring –  Feb 06 '15 at 07:57
  • @MaxSedor: now we are getting to a point we can help you. Please update your question with the code you have so far, the input you give it, the output you expected and what you got instead. – Martijn Pieters Feb 06 '15 at 07:59
  • I accidentally deleted it, but I'll go ahead and do it again. @MartijnPieters –  Feb 06 '15 at 08:00
  • Your loop needs to add up the number of minutes entered for each task. You can do that by doing `total = 0` before the start of the loop, and then inside the loop do `total = total + task`; a more compact way to write that is `total += task` – PM 2Ring Feb 06 '15 at 08:06
  • Thanks for that @PM2Ring I totally forgot about '+=' It now adds them and is just fine with hours, however minutes is not really doing anything... –  Feb 06 '15 at 08:27
  • @MartijnPieters edited the code just need to fix the % part –  Feb 06 '15 at 08:28

1 Answers1

0

You are almost there. Your calculation method is also incorrect; you want the remainder of total and 60, not of hours and 0:

hour = total // 60
minute = total % 60

The remainder of hour and 0 is always going give you a ZeroDivisionError as you cannot divide by 0.

The divmod() function can give you both the hours and the minutes in one step:

hour, minute = divmod(total, 60)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Oh, it most of done that b/c in the code it's inside. –  Feb 06 '15 at 08:30
  • @MaxSedor: perhaps fix that part of your code then? You also forgot to give us some sample input, the output you expected and what you got instead. – Martijn Pieters Feb 06 '15 at 08:31
  • I'll add that, once I edit. I'll get back to you soon as possible! –  Feb 06 '15 at 08:33
  • @MaxSedor: your version will give you an error message. Always include the full traceback for such errors (so starting with `Traceback (most recent call last)`). – Martijn Pieters Feb 06 '15 at 08:34
  • Thank you soo much! It worked!! I typed in 400 minutes then 300 and it added them together to make 700 minutes. Then gave me 11 hours and 40 minutes! Thanks so much! It took me so long to do this and I owe to both of you!!! @MartijnPieters –  Feb 06 '15 at 08:36
  • @MaxSedor: I rolled back your last edit; leaving the actual problem you had in place. That way your question is useful to others in the future. The only thing I ask of you then is to *add in your input and expected output*, as well as the error you got. That way people with the same type of error can find the question and get help too. – Martijn Pieters Feb 06 '15 at 08:41
  • Definitely helped me out! I think I edited it so I shows my sample input and output. @MartijnPieters –  Feb 06 '15 at 08:50
  • @MaxSedor: you did, but you missed out the actual output (e.g. your error). – Martijn Pieters Feb 06 '15 at 08:52
  • Oh, I'll put that afterwards? Saying something like this what happened before? @MartijnPieters –  Feb 06 '15 at 08:54
  • @MaxSedor: Your code, as you finally posted (with the indentation fixed) would lead to a `ZeroDivision` error; you cannot use `% 0` and not get that error. – Martijn Pieters Feb 06 '15 at 09:00
  • Fixed it (I think) @MartijnPieters I might have many future questions, too. But of course, I'll do research and stuff before posting. –  Feb 06 '15 at 09:02
  • Not quite, as now you appear to have working code :-). I'll leave it be, I was mostly trying to teach you how Stack Overflow is supposed to work for these kinds of questions; you post your work with enough info for us to troubleshoot and correct. – Martijn Pieters Feb 06 '15 at 09:32
  • Thanks again though! Now I'll be able to add to the program without any future problems (hopefully)! If I have any questions I'll come here after searching long and hard. @MartijnPieters Obviously you can tell I'm new to this. –  Feb 06 '15 at 22:02
  • I added something when it asks you when you'll start (on the 24-hour clock) that if you enter a number bigger than 24 it asks you to try again, however if you enter the same number or a bigger number it continues on to minutes. It works the same way for minutes too. I'm asking how would I change it so it would keep asking you for a right value if you keep messing up? This is what I currently have: –  Feb 06 '15 at 22:53
  • See http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response – Martijn Pieters Feb 06 '15 at 22:59
  • Tried it and it's saying some of the syntax on there in python 3.4 is invalid @MartijnPieters –  Feb 06 '15 at 23:26
  • @MaxSedor: then perhaps you need to post a new question; but take into account that the code *as posted in that answer* works just fine under Python 3.4. – Martijn Pieters Feb 07 '15 at 00:03
  • I've taken a break from it for a little bit. @MartijnPieters –  Feb 07 '15 at 03:14