checkio练习题:008-sun-angle

Every true traveler must know how to do 3 things: fix the fire, find the water and extract useful information from the nature around him. Programming won't help you with the fire and water, but when it comes to the information extraction - it might be just the thing you need.

Your task is to find the angle of the sun above the horizon knowing the time of the day. Input data: the sun rises in the East at 6:00 AM, which corresponds to the angle of 0 degrees. At 12:00 PM the sun reaches its zenith, which means that the angle equals 90 degrees. 6:00 PM is the time of the sunset so the angle is 180 degrees. If the input will be the time of the night (before 6:00 AM or after 6:00 PM), your function should return - "I don't see the sun!".

Input: The time of the day.

Output: The angle of the sun, rounded to 2 decimal places.

Example:

sun_angle("07:00") == 15 sun_angle("12:15"] == 93.75 sun_angle("01:23") == "I don't see the sun!"

How it is used: One day it can save your life, if you'll be lost far away from civilization.

Precondition: 00:00 <= time <= 23:59

 

def sun_angle(time):
    angle_per_minute = 0.25 # 每分钟对应0.25度
    angle_per_hour = 15  # 每小时对应15度
    time_list = [int(x) for x in time.split(':')]
    if time_list[0] < 6 or time_list[0] >= 18 and time_list[1] > 0:
        return "I don't see the sun!"
    else:
        return (time_list[0] - 6) * angle_per_hour + time_list[1] * angle_per_minute


if __name__ == '__main__':
    print("Example:")
    print(sun_angle("07:10"))

    #These "asserts" using only for self-checking and not necessary for auto-testing
    assert sun_angle("07:00") == 15
    assert sun_angle("01:23") == "I don't see the sun!"
    print("Coding complete? Click 'Check' to earn cool rewards!")

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值