Python help needed cant seem to solve this

I'm suppose to write a code to give me the output of a value between 0 and 86400 and the current time in the 24 hour clock. However I am getting stuck when it comes to writing the formulas for the 24 hour clock and the print function. Here's the code I have written so far.

total_time = float('70000')
hours = int(total_time / 3600.0)
minutes = int((total_time - (hours * 3600.0)) / 60.0)
seconds = int(((total_time) - (hours * 3600) - (minutes * 60)))
print("Enter a value between 0 and 86400", total_time) print("The current time is",hours.minutes.seconds)

2 answers

  • answered 2018-01-13 17:43 Suraj Kothari

    Firstly, get the current hour, minute and seconds:

    import datetime
    
    now = datetime.datetime.now()
    
    # The current time:
    hour = now.hour
    minute = now.minute
    second = now.second
    

    Then simply output it:

    print("Current time: {}:{}:{}".format(hour,minute,second))
    

  • answered 2018-01-13 22:54 BoarGules

    It seems to me you are asking the user to enter a number between 0 and 86400 and then you want to translate that to hh:mm:ss format. But your code is not getting any input from the user, and the last line of the code has syntax errors.

    To get you started, you need to fix your print() calls at the end. Put one statement to a line, and use commas not fullstops:

    print("Enter a value between 0 and 86400", total_time) 
    print("The current time is",hours,minutes,seconds)
    

    That will give you the output:

    Enter a value between 0 and 86400 70000.0
    The current time is 19 26 40
    

    Which is correct, in the sense that an offset of 70,000 seconds from 0h00 today is 19h26m40s.

    If you want to get actual user input, then you need to ask for it, at the top of your program, before you do the calculations, in an input() call:

    total_time=float(input("Enter a value between 0 and 86400: "))
    

    If you want pretty formatting of the answer then do

    print(f"The current time is {hours:02d}:{minutes:02d}:{seconds:02d}")
    

    None of this relates to finding the current time, which Suraj Kothari's answer addresses.