Python Forum
how to think like a computer scientist
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to think like a computer scientist
#1
7. You look at the clock and it is exactly 2pm. You set an alarm to go off in 51 hours. At
what time does the alarm go off? (Hint: you could count on your fingers, but this is not
what we’re after. If you are tempted to count on your fingers, change the 51 to 5100.)

8. Write a Python program to solve the general version of the above problem. Ask the user
for the time now (in hours), and ask for the number of hours to wait. Your program
should output what the time will be on the clock when the alarm goes off.

This was exercise in "how to think like a computer scientist". Can someone help a student out?
Reply
#2
You should try something and then we can assist you with the attempt. Sounds like a basic assignment to teach you about the modulus operator % and writing a basic function.
Reply
#3
current_time_str = input("What is the current time? ")
waiting_time_str = input("How many hours do you have to wait? ")

current_time_int = int(current_time_str)
waiting_time_int = int(waiting_time_str)

hours = current_time_int + waiting_time_int

timeofday = hours % 24

print(timeofday)
This works. However, I am unsure on what to do with the "am" and "pm".
Reply
#4
datetime module has %i (12 hour format) which will display AM or PM
Reply
#5
broke_university_student Wrote:This works. However, I am unsure on what to do with the "am" and "pm".
That would have to do with converting 24-hour time to 12-hour time.
Reply
#6
sorry, you need to combine with %p (which displays AM or PM)
Reply
#7
(Jan-05-2018, 06:39 AM)broke_university_student Wrote:
current_time_str = input("What is the current time? ")
waiting_time_str = input("How many hours do you have to wait? ")

current_time_int = int(current_time_str)
waiting_time_int = int(waiting_time_str)

hours = current_time_int + waiting_time_int

timeofday = hours % 24

print(timeofday)
This works. However, I am unsure on what to do with the "am" and "pm".

I think I found a solution,

current_datetime = input("What is the current time (in hours)? ")
meridien = input('Enter am/pm: ')
waiting_time = input("How many hours do you have to wait? ")

current_time_int = int(current_datetime)
waiting_time_int = int(waiting_time)

hours = current_time_int + waiting_time_int

timeofday = hours % 24


print(timeofday,meridien)
buran write Dec-03-2020, 08:38 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020