171k views
1 vote
Many people keep time using a 24 hour clock (11 is 11am and 23 is 11pm, 0 is midnight). If it is currently 13 and you set your alarm to go off in 50 hours, it will be 15 (3pm). Write a Python program to solve the general version of the above problem. Ask the user for the time now (in hours), and then ask for the number of hours to wait for the alarm. Your program should output what the time will be on the clock when the alarm goes off.

User Gene S
by
7.6k points

1 Answer

1 vote

# Ask the user for the current time in hours (using a 24-hour clock)

current_time = int(input("What is the current time (in hours)? "))

# Ask the user for the number of hours to wait for the alarm

hours_to_wait = int(input("How many hours do you want to wait for the alarm? "))

# Calculate the time on the clock when the alarm goes off

alarm_time = (current_time + hours_to_wait) % 24

# Print the result

print("The alarm will go off at", alarm_time, "hours.")

##or to get the time from the device clock use this code : ##

import datetime

# Get the current time from the device clock

current_time = datetime.datetime.now().hour

# Ask the user for the number of hours to wait for the alarm

hours_to_wait = int(input("How many hours do you want to wait for the alarm? "))

# Calculate the time on the clock when the alarm goes off

alarm_time = (current_time + hours_to_wait) % 24

# Print the result

print("The alarm will go off at", alarm_time, "hours.")

User Jclangst
by
7.2k points