Решение на Астрологични забави от Никола Терзиев

Обратно към всички решения

Към профила на Никола Терзиев

Резултати

  • 10 точки от тестове
  • 0 бонус точки
  • 10 точки общо
  • 6 успешни тест(а)
  • 0 неуспешни тест(а)

Код

import calendar
def check_day_month_pair(day, month):
if not isinstance(day, int) or not isinstance(month, int):
raise TypeError('Day and month should be int')
if (day < 1 or day > 31) or (month < 1 or month > 12):
raise ValueError('Your day is way off')
if day > calendar.monthrange(2000, month)[1]:
raise ValueError('There is no such day in given month')
def interpret_western_sign(day, month):
check_day_month_pair(day, month)
if (month == 1 and day >= 21) or (month == 2 and day <= 19):
return 'aquarius'
elif (month == 2 and day >= 20) or (month == 3 and day <= 20):
return 'pisces'
elif (month == 3 and day >= 21) or (month == 4 and day <= 20):
return 'aries'
elif (month == 4 and day >= 21) or (month == 5 and day <= 20):
return 'taurus'
elif (month == 5 and day >= 21) or (month == 6 and day <= 20):
return 'gemini'
elif (month == 6 and day >= 21) or (month == 7 and day <= 22 ):
return 'cancer'
elif (month == 7 and day >= 23) or (month == 8 and day <= 22):
return 'leo'
elif (month == 8 and day >= 23) or (month == 9 and day <= 22):
return 'virgo'
elif (month == 9 and day >= 23) or (month == 10 and day <= 22):
return 'libra'
elif (month == 10 and day >= 23) or (month == 11 and day <= 21):
return 'scorpio'
elif (month == 11 and day >= 22) or (month == 12 and day <= 21):
return 'sagittarius'
elif (month == 12 and day >= 22) or (month == 1 and day <= 20):
return 'capricorn'
else: return ''
def interpret_chinese_sign(year):
chinese_zodiacs = ['rat', 'ox', 'tiger', 'rabbit', 'dragon', 'snake', 'horse', 'sheep', 'monkey', 'rooster', 'dog' 'pig']
year -= 1900
return chinese_zodiacs[year % 12]
def interpret_both_signs(day, month, year):
return (interpret_western_sign(day, month), interpret_chinese_sign(year))

Лог от изпълнението

......
----------------------------------------------------------------------
Ran 6 tests in 0.004s

OK

История (1 версия и 1 коментар)

Никола обнови решението на 11.03.2015 11:52 (преди над 9 години)

+import calendar
+
+
+def check_day_month_pair(day, month):
+ if not isinstance(day, int) or not isinstance(month, int):
+ raise TypeError('Day and month should be int')
+ if (day < 1 or day > 31) or (month < 1 or month > 12):
+ raise ValueError('Your day is way off')
+ if day > calendar.monthrange(2000, month)[1]:
+ raise ValueError('There is no such day in given month')
+
+
+def interpret_western_sign(day, month):
+ check_day_month_pair(day, month)
+
+ if (month == 1 and day >= 21) or (month == 2 and day <= 19):
+ return 'aquarius'
+ elif (month == 2 and day >= 20) or (month == 3 and day <= 20):
+ return 'pisces'
+ elif (month == 3 and day >= 21) or (month == 4 and day <= 20):
+ return 'aries'
+ elif (month == 4 and day >= 21) or (month == 5 and day <= 20):
+ return 'taurus'
+ elif (month == 5 and day >= 21) or (month == 6 and day <= 20):
+ return 'gemini'
+ elif (month == 6 and day >= 21) or (month == 7 and day <= 22 ):
+ return 'cancer'
+ elif (month == 7 and day >= 23) or (month == 8 and day <= 22):
+ return 'leo'
+ elif (month == 8 and day >= 23) or (month == 9 and day <= 22):
+ return 'virgo'
+ elif (month == 9 and day >= 23) or (month == 10 and day <= 22):
+ return 'libra'
+ elif (month == 10 and day >= 23) or (month == 11 and day <= 21):
+ return 'scorpio'
+ elif (month == 11 and day >= 22) or (month == 12 and day <= 21):
+ return 'sagittarius'
+ elif (month == 12 and day >= 22) or (month == 1 and day <= 20):
+ return 'capricorn'
+ else: return ''
+
+def interpret_chinese_sign(year):
+ chinese_zodiacs = ['rat', 'ox', 'tiger', 'rabbit', 'dragon', 'snake', 'horse', 'sheep', 'monkey', 'rooster', 'dog' 'pig']
+ year -= 1900
+
+ return chinese_zodiacs[year % 12]
+
+def interpret_both_signs(day, month, year):
+ return (interpret_western_sign(day, month), interpret_chinese_sign(year))
+
  • Всеки ред трябва да е до 79 символа
  • else: return '' е много кофти, чете се значително по-трудно от колкото ако има нов ред
  • Валидацията е напълно ненужна, но щом те влече :)
  • Цялото if/elif/else нещо може да стане доста по-елегантно, но може би няма много време за това вече. :)