-3
hr = int(input("Enter current hr:"))
min= int(input("Enter current min:"))
sec = int(input("Enter current sec:"))
print("Clock time now is",hr,":",min,":",sec)
sec= sec+1
print("After 1 second, the time is",hr,":",min,":",sec)

how do i also limit the input so to a certain range? like the input for hours only accepts range from 0-23 and mins 0-50 secs 0-59? and how do i make the sec + 1 to automatically update the end time for hrs and mins? lets say 23 : 59 : 59+1 = 00:00:00

  • 1
    Maybe this can help you: https://stackoverflow.com/questions/100210/what-is-the-standard-way-to-add-n-seconds-to-datetime-time-in-python – Daniser Jul 27 '20 at 16:41
  • Does this answer your question? [What is the standard way to add N seconds to datetime.time in Python?](https://stackoverflow.com/questions/100210/what-is-the-standard-way-to-add-n-seconds-to-datetime-time-in-python) – stovfl Jul 27 '20 at 18:43

1 Answers1

0

You can use datetime package for achieving this. Although you can't stop user from entering an incorrect value, But the same can be raised as an error though try-exception.

from datetime import time
time_input = input('Enter a time in the format HH:MM:SS \n')
# input 15:25:23
try:
    h, m, s = map(int, time_input.split(':'))
    res = time(hour=h, minute=m, second=s+1)
    print(res)
except ValueError as e:
    print(e)
Dharman
  • 30,962
  • 25
  • 85
  • 135
Raghul Raj
  • 1,428
  • 9
  • 24