'Zodiac sign with Python using dict [closed]

could u please help, it least the beginning, where i need to start. the task is

Enter the month number (1-12) of your date of birth: 7 (for example)
Enter the number of your date of birth: 4
Your zodiac sign: Aries

zodiac = {
    "aries": '21.03-20.04',
    "taurus": '21.04-21.05',
    "gemini": '22.05-21.06',
    "cancer": '22.06-22.07',
    "lio": '23.07-22.08',
    "virgo": '23.08-23.09',
    "libra": '24.09-23.10',
    "scorpio": '24.10-22.11',
    "sagittarius": '23.11-21.12',
    "capricorn": '22.12-20.01',
    "aquarius": '21.01-18.02',
    "pisces": '19.02-20.03',
}

To check the validity of the date, i will also need another dictionary, with a max. number of days per month

i did this task with if/elif - its ok for me, but using dict....a little problem



Solution 1:[1]

I've put the max number of days per month as another dictionary. One way you can achieve this:

zodiac = {
    "aries": '21.03-20.04',
    "taurus": '21.04-21.05',
    "gemini": '22.05-21.06',
    "cancer": '22.06-22.07',
    "lio": '23.07-22.08',
    "virgo": '23.08-23.09',
    "libra": '24.09-23.10',
    "scorpio": '24.10-22.11',
    "sagittarius": '23.11-21.12',
    "capricorn": '22.12-20.01',
    "aquarius": '21.01-18.02',
    "pisces": '19.02-20.03',
}

month_days = {
    1 : 31,
    2 : 28,
    3 : 31,
    4 : 30,
    5:31,
    6:30,
    7:31,
    8:31,
    9:30,
    10:31,
    11:30,
    12:31
}

month = input("Enter the month number (1-12) of your date of birth:")
day = input("Enter the number of your date of birth:")

def convert_to_days(month, d):
    days = 0
    for i in range(1,int(month)):
        days += d[i]
    return days

user_zodiac = ""

for item in zodiac.items():
    limits = item[1].split("-")
    day_and_month = [limit.split(".") for limit in limits]

    if (int(day_and_month[0][0].lstrip("0"))+convert_to_days(int(day_and_month[0][1].lstrip("0")), month_days)) \
        <= int(day) + convert_to_days(month, month_days) <= \
        (int(day_and_month[1][0].lstrip("0"))+convert_to_days(int(day_and_month[1][1].lstrip("0")), month_days)):
        user_zodiac = item[0]
        break


if user_zodiac == "":
    user_zodiac = "capricorn"

print(f"Your zodiac sign is {user_zodiac}")

Essentially what we do is convert the months into days (i.e. maximum of 365 for 31.12) and check if the entered month and day falls between certain ranges.

Note that since you are entering in month number first and date second, a person with a birthday on July 4th has the sign of Cancer, not Aries, which would be if you had a birthday on April 7th.

Output:

Enter the month number (1-12) of your date of birth:7
Enter the number of your date of birth:4
Your zodiac sign is cancer

Enter the month number (1-12) of your date of birth:11
Enter the number of your date of birth:22
Your zodiac sign is scorpio

Enter the month number (1-12) of your date of birth:2
Enter the number of your date of birth:14
Your zodiac sign is aquarius

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1