Hello, today I have almost completed all the basic questions on py.checkio.org and are ready to move into the real game logic programming. One of the questions I have solved today is the question which asks us to find out the angle of the sun based on the position of the clock. For example, 12.pm noon means 90 degrees, 6.00 am morning means 0 degrees and we will not see the sun after 6.00pm and before 6.00 am. We need to create a python function which will take in a time string and then return the angle of the sun based on the position of the clock. If we do not see the sun yet then just return a text telling the user about that. After a simple calculation, we know that the angle between each of the two subsequent hours is 15 degrees. Here is what the sun angle looks like from a simple sketch.
Below is the program which I have came out with to solve the above problem.
def sun_angle(time): hour, minute = time.split(':') # split the time into hour and minute t_h = int(hour) t_m = int(minute) time = t_h + t_m/60 if((time< 6.00) or (time > 18.00)): return "I don't see the sun!" if(t_h < 12): t_h = (t_h - 6) * 15 else: t_h = 90 + (t_h - 12) * 15 if(t_m != 0): t_m = t_m * 15/60 return t_h + t_m else: return t_h
Some questions in this site are really tricky so we will need some times to solve them.
from Planet Python
via read more
No comments:
Post a Comment