Issue
Write a program that asks the user for an hour between 1 and 12 and for how many hours in the future they want to go. Print out what the hour will be that many hours into the future. An example is shown below. I know theres a simple way to do this. I need help.
hrs = eval(input('ENTER HOUR: '))
ahead = eval(input('HOW MANY HOURS AHEAD: '))
new_hour = hrs+ahead
a = 1
b = 2
c = 3
d = 4
e = 5
f = 6
g = 7
h = 8
i = 9
j = 10
k = 11
l = 12
if new_hour <= 12:
print(hrs+ahead)
if new_hour == 13:
print(a)
if new_hour == 14:
print(b)
if new_hour == 15:
print(c)
if new_hour == 16:
print(d)
if new_hour == 17:
print(e)
if new_hour == 18:
print(f)
if new_hour == 19:
print(g)
if new_hour == 20:
print(h)
if new_hour == 21:
print(i)
if new_hour == 22:
print(j)
if new_hour == 23:
print(k)
if new_hour == 24:
print(l)
Solution
This would be best done with the modulo operator. It appears that you want to get the time in a 12-hour format, so an implementation may be:
if new_hour % 12 == 0:
print(12)
else:
print(new_hour % 12)
This can be further simplified to a single ternary assignment. An example of this could be:
new_hour = new_hour % 12 if new_hour % 12 != 0 else 12
print(new_hour)
Answered By - bbnumber2
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.