w3resource

Python: Convert all units of time into seconds


Time to Seconds Converter

Write a Python program to convert all units of time into seconds.

Pictorial Presentation:

Convert all units of time into seconds

Sample Solution:-

Python Code:

# Prompt the user to input a number of days and store it in the variable 'days'.
days = int(input("Input days: ")) * 3600 * 24
# Prompt the user to input a number of hours and store it in the variable 'hours'.
hours = int(input("Input hours: ")) * 3600
# Prompt the user to input a number of minutes and store it in the variable 'minutes'.
minutes = int(input("Input minutes: ")) * 60
# Prompt the user to input a number of seconds and store it in the variable 'seconds'.
seconds = int(input("Input seconds: "))
# Calculate the total time in seconds by adding the converted values.
time = days + hours + minutes + seconds
# Print the total time in seconds.
print("The amount of seconds:", time)

Sample Output:

Input days:  4
Input hours:  5
Input minutes:  20
Input seconds:  10
The  amounts of seconds 364810

Flowchart:

Flowchart: Convert all units of time into seconds.

For more Practice: Solve these Related Problems:

  • Write a Python program to convert a given number of seconds into weeks, days, hours, minutes, and seconds.
  • Write a Python program to calculate the total number of seconds in a given number of months (assuming 30 days per month).
  • Write a Python program to take a duration in hours, minutes, and seconds and convert it into milliseconds.
  • Write a Python program to convert a given number of microseconds into hours, minutes, and seconds.

Python Code Editor:

 

Previous: Write a Python program to convert the distance (in feet) to inches, yards, and miles.
Next: Write a Python program to get an absolute file path.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.