Skip to main content

Section 2.11 Using Input in a Program

Now that we have the final puzzle piece in place, we can write the code that lets users choose any number of seconds they like, and we will tell them how many hours, minutes, and seconds that works out to.
Here is an activecode area with the pseudo-code in comments. Put in the Python code and test your program to see that it gives the correct results.

Checkpoint 2.11.1.

Solution.
# Use the input function to read number of seconds into str_total_seconds
# Use the int function to convert str_total_seconds to total_seconds
str_total_seconds = input("Enter number of seconds: ")
total_seconds = int(str_total_seconds)

# Calculate hours as the quotient of dividing total_seconds and 3600
hours = total_seconds // 3600

# Calculate seconds_remaining as the remainder of dividing total_seconds and 3600.
seconds_remaining = total_seconds % 3600

# Calculate minutes as the quotient of dividing seconds_remaining by 60.
minutes = seconds_remaining // 60

# Calculate seconds_finally_remaining as the remainder of dividing
# seconds_remaining by 60.
seconds_finally_remaining = seconds_remaining % 60

# Print hours, minutes, and seconds_finally_remaining,
# properly labeled.
print(hours, "hours", minutes, "minutes and", seconds_finally_remaining, "seconds.")