Skip to content

Zodiac Sign Predictor

Abstract

Zodiac Sign Predictor is a Python project that predicts zodiac signs based on birth dates. The application features date parsing, sign calculation, and a CLI interface, demonstrating best practices in automation and user interaction.

Prerequisites

  • Python 3.8 or above
  • A code editor or IDE
  • Basic understanding of date parsing and user input
  • Required libraries: None (standard library only)

Before you Start

No external libraries required.

Getting Started

Create a Project

  1. Create a folder named zodiac-sign-predictorzodiac-sign-predictor.
  2. Open the folder in your code editor or IDE.
  3. Create a file named zodiac_sign_predictor.pyzodiac_sign_predictor.py.
  4. Copy the code below into your file.

Write the Code

⚙️ Zodiac Sign Predictor
Zodiac Sign Predictor
class ZodiacSignPredictor:
    def __init__(self):
        self.signs = [
            (1, 20, 'Aquarius'), (2, 19, 'Pisces'), (3, 21, 'Aries'), (4, 20, 'Taurus'),
            (5, 21, 'Gemini'), (6, 21, 'Cancer'), (7, 23, 'Leo'), (8, 23, 'Virgo'),
            (9, 23, 'Libra'), (10, 23, 'Scorpio'), (11, 22, 'Sagittarius'), (12, 22, 'Capricorn')
        ]
 
    def predict(self, month, day):
        for m, d, sign in self.signs:
            if (month == m and day >= d) or (month == m % 12 + 1 and day < self.signs[m % 12][1]):
                print(f"Zodiac sign: {sign}")
                return sign
        print("Invalid date.")
        return None
 
    def demo(self):
        self.predict(3, 25)
        self.predict(7, 30)
 
if __name__ == "__main__":
    print("Zodiac Sign Predictor Demo")
    predictor = ZodiacSignPredictor()
    predictor.demo()
 
Zodiac Sign Predictor
class ZodiacSignPredictor:
    def __init__(self):
        self.signs = [
            (1, 20, 'Aquarius'), (2, 19, 'Pisces'), (3, 21, 'Aries'), (4, 20, 'Taurus'),
            (5, 21, 'Gemini'), (6, 21, 'Cancer'), (7, 23, 'Leo'), (8, 23, 'Virgo'),
            (9, 23, 'Libra'), (10, 23, 'Scorpio'), (11, 22, 'Sagittarius'), (12, 22, 'Capricorn')
        ]
 
    def predict(self, month, day):
        for m, d, sign in self.signs:
            if (month == m and day >= d) or (month == m % 12 + 1 and day < self.signs[m % 12][1]):
                print(f"Zodiac sign: {sign}")
                return sign
        print("Invalid date.")
        return None
 
    def demo(self):
        self.predict(3, 25)
        self.predict(7, 30)
 
if __name__ == "__main__":
    print("Zodiac Sign Predictor Demo")
    predictor = ZodiacSignPredictor()
    predictor.demo()
 

Example Usage

Run zodiac predictor
python zodiac_sign_predictor.py
Run zodiac predictor
python zodiac_sign_predictor.py

Explanation

Key Features

  • Date Parsing: Parses birth dates from user input.
  • Sign Calculation: Determines zodiac sign based on date.
  • Error Handling: Validates inputs and manages exceptions.
  • CLI Interface: Interactive command-line usage.

Code Breakdown

  1. Date Parsing and Sign Calculation Functions
zodiac_sign_predictor.py
def get_zodiac_sign(day, month):
    zodiac_dates = [
        (1, 20, 'Capricorn'), (2, 19, 'Aquarius'), (3, 21, 'Pisces'),
        (4, 20, 'Aries'), (5, 21, 'Taurus'), (6, 21, 'Gemini'),
        (7, 23, 'Cancer'), (8, 23, 'Leo'), (9, 23, 'Virgo'),
        (10, 23, 'Libra'), (11, 22, 'Scorpio'), (12, 22, 'Sagittarius'),
        (12, 31, 'Capricorn')
    ]
    for m, d, sign in zodiac_dates:
        if (month == m and day >= d) or (month == m+1 and day < zodiac_dates[m%12][1]):
            return sign
    return 'Unknown'
zodiac_sign_predictor.py
def get_zodiac_sign(day, month):
    zodiac_dates = [
        (1, 20, 'Capricorn'), (2, 19, 'Aquarius'), (3, 21, 'Pisces'),
        (4, 20, 'Aries'), (5, 21, 'Taurus'), (6, 21, 'Gemini'),
        (7, 23, 'Cancer'), (8, 23, 'Leo'), (9, 23, 'Virgo'),
        (10, 23, 'Libra'), (11, 22, 'Scorpio'), (12, 22, 'Sagittarius'),
        (12, 31, 'Capricorn')
    ]
    for m, d, sign in zodiac_dates:
        if (month == m and day >= d) or (month == m+1 and day < zodiac_dates[m%12][1]):
            return sign
    return 'Unknown'
  1. CLI Interface and Error Handling
zodiac_sign_predictor.py
def main():
    print("Zodiac Sign Predictor")
    while True:
        cmd = input('> ')
        if cmd == 'predict':
            try:
                day = int(input("Day: "))
                month = int(input("Month: "))
                print(get_zodiac_sign(day, month))
            except Exception as e:
                print(f"Error: {e}")
        elif cmd == 'exit':
            break
        else:
            print("Unknown command. Type 'predict' or 'exit'.")
 
if __name__ == "__main__":
    main()
zodiac_sign_predictor.py
def main():
    print("Zodiac Sign Predictor")
    while True:
        cmd = input('> ')
        if cmd == 'predict':
            try:
                day = int(input("Day: "))
                month = int(input("Month: "))
                print(get_zodiac_sign(day, month))
            except Exception as e:
                print(f"Error: {e}")
        elif cmd == 'exit':
            break
        else:
            print("Unknown command. Type 'predict' or 'exit'.")
 
if __name__ == "__main__":
    main()

Features

  • Zodiac Prediction: Date parsing and sign calculation
  • Modular Design: Separate functions for each task
  • Error Handling: Manages invalid inputs and exceptions
  • Production-Ready: Scalable and maintainable code

Next Steps

Enhance the project by:

  • Integrating with user profile datasets
  • Supporting horoscope generation
  • Creating a GUI for prediction
  • Adding batch prediction
  • Unit testing for reliability

Educational Value

This project teaches:

  • User Interaction: Date parsing and logic
  • Software Design: Modular, maintainable code
  • Error Handling: Writing robust Python code

Real-World Applications

  • Entertainment Apps
  • User Profiling
  • Automation Tools

Conclusion

Zodiac Sign Predictor demonstrates how to build a scalable and accurate zodiac prediction tool using Python. With modular design and extensibility, this project can be adapted for real-world applications in entertainment, automation, and more. For more advanced projects, visit Python Central Hub.

Was this page helpful?

Let us know how we did