Respuesta :
Answer:
print("Welcome to the Time Conversion Calculator")
#you need to check for each if user provided each of this inputs
while(True) :
try:
no_of_hours = int(input("Enter a number of hours:"))
if type(no_of_hours) is int:
break
else:
print('invalid input\n')
no_of_hours = input('"Enter a number of hours:"\n')
except ValueError:
print('invalid amount\n')
no_of_hours = input('"Enter a number of hours:"')
while(True) :
try:
no_of_minutes = int(input("Enter a number of minutes:"))
if type(no_of_minutes) is int:
break
else:
print('invalid input\n')
no_of_minutes = input('"Enter a number of minutes:"\n')
except ValueError:
print('invalid amount\n')
no_of_minutes = input('"Enter a number of minutes:"')
while(True) :
try:
no_of_seconds = int(input("Enter a number of seconds:"))
if type(no_of_seconds) is int:
break
else:
print('invalid input\n')
no_of_seconds = input('"Enter a number of seconds:"\n')
except ValueError:
print('invalid input\n')
no_of_seconds = input('"Enter a number of seconds:"')
# variables to keep track of days hours minutes
no_of_days = 0
days = False
hours = False
minutes = False
seconds = False
hours_unit = "hours"
minutes_unit = "minutes"
seconds_unit = "seconds"
days_unit = "days"
#calculated values with initial values of zero
calc_minutes = 0
calc_hours = 0
if no_of_seconds >= 60:
minutes= True
calc_minutes = no_of_seconds // 60
remaining_seconds = no_of_seconds - (calc_minutes * 60)
if remaining_seconds > 0:
no_of_seconds = remaining_seconds
seconds = True
if no_of_seconds > 1:
seconds_unit = "seconds"
else:
seconds_unit = "second"
no_of_minutes = no_of_minutes + calc_minutes
if no_of_minutes >= 60:
hours = True
calc_hours = no_of_minutes // 60
remaining_minutes = no_of_minutes - (calc_hours * 60)
if remaining_minutes > 0:
no_of_minutes = remaining_minutes
minutes = True
if no_of_minutes > 1:
minutes_unit = "minutes"
else:
minutes_unit = "minute"
no_of_hours = no_of_hours + calc_hours
if no_of_hours >= 24:
days = True
no_of_days = no_of_hours // 24
remaining_hours = no_of_hours - (no_of_days * 24)
if no_of_days > 1:
days_unit = "days"
else:
days_unit = "day"
if remaining_hours > 0:
no_of_hours = remaining_hours
minutes = True
if no_of_hours > 1:
hours_unit = "hours"
else:
hours_unit = "hour"
if no_of_days:
print(str(no_of_days)+ " " + days_unit)
if no_of_hours:
print(str(no_of_hours)+ " " + hours_unit)
if no_of_minutes:
print(str(no_of_minutes)+ " " + minutes_unit)
if no_of_seconds:
print(str(no_of_seconds)+ " " + seconds_unit)
print("Goodbye!")
Explanation:
This implementation is with the python programming language but can be rewritten in any language of your choose.
The program first forces users to provide the correct input using try catch.
it accepts the input, attempts to convert to int. if it fails, it repromts the user to enter the correct input
After uses boolean values to determine whether a value for seconds, minutes and hour is present inthe the question.
The calc values refers to the calculated values during runtime of minutes, hour and days
it then adds this calc values to the user inputs