题目
我的代码
思路
忽略硬编码,正则取小时,分钟,判断范围,求角度
import re
def sun_angle(time):
reg = re.compile('(?P<hour>\d+):(?P<minute>\d+)')
regMatch = reg.match(time)
timeDict = regMatch.groupdict()
hour = int(timeDict['hour'])
minute = int(timeDict['minute'])
if hour == 18 and minute == 0:
return 180
elif hour < 6 or hour >= 18:
return 'I don\'t see the sun!'
else:
return ((hour - 6)*60 + minute)*0.25
if __name__ == '__main__':
print("Example:")
print(sun_angle("07:00"))
#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!")