-1

I'm trying to breakdown a users integer input into hours, minutes, and seconds for a 24 hour period but having issues with going from pseudo code to actual code past the first hour equation. I want the final output to be in X hours, Y minutes, and Z seconds:

    day = 86400
    hour = 3600 #1 hour in a day * 60min * 60 sec
    minute = 60
    number = input("Choose a number between 0 and 86400: ")
    while number != 0:
        if number > 0:
            newNumber = number / hour
            number = newNumber

I'm teaching myself coding so I would love a simple approach to this problem... Am I even on the right track?

EDIT: I know there is a duplicate version of this question but that one simplifies things a bit too much (ironic) for me. I'm trying to learn by incremental steps but I do appreciate all the feedback

adamcasey
  • 9
  • 6
  • 2
    You need to use floor division `//` and modulus `%` or `divmod` but take a look at http://stackoverflow.com/questions/775049/python-time-seconds-to-hms – PM 2Ring Jan 24 '17 at 03:24
  • `#!/usr/bin/env python3; # day = 86400; # not used hour = 3600; minute = 60; number = int(input("Choose a number between 0 and 86400: ")); hours = number // hour; minutes = (number % hour) // 60; seconds = number % minutes; print('hours: {h} minutes: {m} seconds: {s}'.format(h=hours, m=minutes, s=seconds));` – chickity china chinese chicken Jan 24 '17 at 05:49

1 Answers1

0

To convert a users input into X hours Y minute and Z seconds it depends on what they're inputting. If it's seconds, as it seems to be above, then do as the above says and take the seconds and divide it by 3600 for your hours, then subtract that number from the beginning number.

Also, it looks like you're using an infinite while loop, as you aren't prompting for input each time around.

If you want to do what you're trying to do I would suggest this:

number = input("Choose a number between 0 and 86400")
while(number != 0)
    hours = number/3600
    number = number-(hours*3600)
    minutes = number/60
    number = number-(minutes*60)
    seconds = number
    number = 0
print("The Time You Entered was " + str(hours) + " hours, " + str(minutes) + " minutes, and " + str(seconds) " seconds.")
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
Zach Handley
  • 914
  • 1
  • 12
  • 25
  • You don't need that `while` loop. And you should use the `//` floor division operator to make this work properly on Python 3 and Python 2. Of course, in Python 3 you'd need to convert the input string to integer with `int()`. And in Python 2 you really should use `raw_input` and convert using `int()` too, rather than using the unsafe Python 2 `input()` function which evaluates the user input. For details, please see [Eval really is dangerous](http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html) by SO veteran Ned Batchelder. – PM 2Ring Jan 24 '17 at 04:43