Back to Freecodecamp

Challenge 174: Zodiac Finder

curriculum/challenges/english/blocks/daily-coding-challenges-python/69738771fb5a7b8b24cca2a0.md

latest3.6 KB
Original Source

--description--

Given a date string in the format "YYYY-MM-DD", return the zodiac sign for that date using the following chart:

Date RangeZodiac Sign
March 21 - April 19"Aries"
April 20 - May 20"Taurus"
May 21 - June 20"Gemini"
June 21 - July 22"Cancer"
July 23 - August 22"Leo"
August 23 - September 22"Virgo"
September 23 - October 22"Libra"
October 23 - November 21"Scorpio"
November 22 - December 21"Sagittarius"
December 22 - January 19"Capricorn"
January 20 - February 18"Aquarius"
February 19 - March 20"Pisces"
  • Zodiac signs are based only on the month and day, you can ignore the year.

--hints--

get_sign("2026-01-31") should return "Aquarius".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_sign("2026-01-31"), "Aquarius")`)
}})

get_sign("2001-06-10") should return "Gemini".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_sign("2001-06-10"), "Gemini")`)
}})

get_sign("1985-09-07") should return "Virgo".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_sign("1985-09-07"), "Virgo")`)
}})

get_sign("2023-03-19") should return "Pisces".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_sign("2023-03-19"), "Pisces")`)
}})

get_sign("2045-11-05") should return "Scorpio".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_sign("2045-11-05"), "Scorpio")`)
}})

get_sign("1985-12-06") should return "Sagittarius".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_sign("1985-12-06"), "Sagittarius")`)
}})

get_sign("2025-12-30") should return "Capricorn".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_sign("2025-12-30"), "Capricorn")`)
}})

get_sign("2018-10-08") should return "Libra".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_sign("2018-10-08"), "Libra")`)
}})

get_sign("1958-05-04") should return "Taurus".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_sign("1958-05-04"), "Taurus")`)
}})

--seed--

--seed-contents--

py
def get_sign(date_str):

    return date_str

--solutions--

py
def get_sign(date_str):
    _, month_str, day_str = date_str.split("-")
    month = int(month_str)
    day = int(day_str)

    if (month == 3 and day >= 21) or (month == 4 and day <= 19):
        return "Aries"
    if (month == 4 and day >= 20) or (month == 5 and day <= 20):
        return "Taurus"
    if (month == 5 and day >= 21) or (month == 6 and day <= 20):
        return "Gemini"
    if (month == 6 and day >= 21) or (month == 7 and day <= 22):
        return "Cancer"
    if (month == 7 and day >= 23) or (month == 8 and day <= 22):
        return "Leo"
    if (month == 8 and day >= 23) or (month == 9 and day <= 22):
        return "Virgo"
    if (month == 9 and day >= 23) or (month == 10 and day <= 22):
        return "Libra"
    if (month == 10 and day >= 23) or (month == 11 and day <= 21):
        return "Scorpio"
    if (month == 11 and day >= 22) or (month == 12 and day <= 21):
        return "Sagittarius"
    if (month == 12 and day >= 22) or (month == 1 and day <= 19):
        return "Capricorn"
    if (month == 1 and day >= 20) or (month == 2 and day <= 18):
        return "Aquarius"
    if (month == 2 and day >= 19) or (month == 3 and day <= 20):
        return "Pisces"