Python code to know the zodiac sign based on the provided birthday.
Tags: python, codenewbie, programming
Published: 2024-05-14 19:23:00

This Python code determines the zodiac sign based on the provided birthday in the format “DD-MM”. For each zodiac sign, corresponding date ranges are defined. Each range is represented as a tuple with a start and end date, along with the name of the zodiac sign.

After the user inputs their birthday, the main function get_zodiac_sign parses the date into two integers (day and month). Then it checks which date range the provided date falls into, using the defined ranges for each zodiac sign. If the provided date does not match any of the specified ranges, the function returns “Unknown”.

After that, the function is called to determine the zodiac sign for the entered birthday, and the result is displayed on the screen.

```
def get_zodiac_sign(birthday):
    # Parse the birthday string
    day, month = map(int, birthday.split('-'))

    # Define period ranges and corresponding names
    period_ranges = [
        ((21, 3), (19, 4), "Aries"),
        ((20, 4), (20, 5), "Taurus"),
        ((21, 5), (21, 6), "Gemini"),
        ((22, 6), (22, 7), "Cancer"),
        ((23, 7), (22, 8), "Leo"),
        ((23, 8), (22, 9), "Virgo"),
        ((23, 9), (23, 10), "Libra"),
        ((24, 10), (21, 11), "Scorpius"),
        ((22, 11), (21, 12), "Sagittarius"),
        ((22, 12), (19, 1), "Capricornus"),
        ((20, 1), (18, 2), "Aquarius"),
        ((19, 2), (20, 3), "Pisces")
    ]
   
    print(*period_ranges, sep='\n')

    # Convert the birthday to a tuple for comparison
    birthday_tuple = (day, month)

    # Check which period the birthday falls into
    for start, end, period_name in period_ranges:
        start_day, start_month = start
        end_day, end_month = end
        if (start_month < month < end_month) or \
           (start_month == month and day >= start_day) or \
           (end_month == month and day <= end_day):
            return period_name
    return "Unknown"  # Default if date doesn't match any range

# Get the birthday from the user
birthday = input("Enter your birthday (DD-MM): ")

# Call the function to get the corresponding period
period = get_zodiac_sign(birthday)
print("Your zodiac sign based on your birthday", birthday, "is:", period)
```

Example:

```
dmi@dmi-laptop:~/my_python$ python3 zodiac.py 
Enter your birthday (DD-MM): 01-04
((21, 3), (19, 4), 'Aries')
((20, 4), (20, 5), 'Taurus')
((21, 5), (21, 6), 'Gemini')
((22, 6), (22, 7), 'Cancer')
((23, 7), (22, 8), 'Leo')
((23, 8), (22, 9), 'Virgo')
((23, 9), (23, 10), 'Libra')
((24, 10), (21, 11), 'Scorpius')
((22, 11), (21, 12), 'Sagittarius')
((22, 12), (19, 1), 'Capricornus')
((20, 1), (18, 2), 'Aquarius')
((19, 2), (20, 3), 'Pisces')
Your zodiac sign based on your birthday 01-04 is: Aries
dmi@dmi-laptop:~/my_python$ python3 zodiac.py 
Enter your birthday (DD-MM): 04-09
((21, 3), (19, 4), 'Aries')
((20, 4), (20, 5), 'Taurus')
((21, 5), (21, 6), 'Gemini')
((22, 6), (22, 7), 'Cancer')
((23, 7), (22, 8), 'Leo')
((23, 8), (22, 9), 'Virgo')
((23, 9), (23, 10), 'Libra')
((24, 10), (21, 11), 'Scorpius')
((22, 11), (21, 12), 'Sagittarius')
((22, 12), (19, 1), 'Capricornus')
((20, 1), (18, 2), 'Aquarius')
((19, 2), (20, 3), 'Pisces')
Your zodiac sign based on your birthday 04-09 is: Virgo
dmi@dmi-laptop:~/my_python$
```

`ask_dima@yahoo.com`