0

I'm working on a mini horoscope theme-based project where I ask for the users' birth day and month and as a result output their zodiac sign. So far my code is simply printing the day and month and my entire CSV file. I'm trying to print the specific outcome based on what the user enters. Here's what I have so far.

import csv


def main():
    fields = []
    rows = []
    with open('TableHoroscope.csv', 'r') as csvfile:
        csvreader = csv.reader(csvfile)
        fields = next(csvreader)
        for row in csvreader:
            rows.append(row)
        print("Num of rows:", csvreader.line_num)
    print('Field names are:' + ', '.join(field for field in fields))

    user_birthday()
    print_zodiac(rows)


def print_zodiac(rows):
    for row in rows[:13]:
        for col in row:
            print(col)
        print('')


def user_birthday():
    date = int(input("Enter date:"))
    month = input("Enter month:")
    print('Birthday: ' + str(date) + month)
    print('')


main()

Here is a text version of my CSV file

SIGN,START,END
Aries,21-Mar,19-Apr
Taurus,20-Apr,20-May
Gemini,21-May,20-Jun
Cancer,21-Jun,22-Jul
Leo,23-Jul,22-Aug
Virgo,23-Aug,22-Sep
Libra,23-Sep,22-Oct
Scorpio,23-Oct,21-Nov
Sagittarius,22-Nov,21-Dec
Capricorn,22-Dec,19-Jan
Aquarius,20-Jan,18-Feb
Pisces,19-Feb,20-Mar

I would really like help on how to approach this. I'm limited to what I can do with my code considering I need to have four def functions and no global variables and I'm still a beginner. Thank you!

2
  • 2
    You should provide the CSV file as text (rather than an image) to more easily allow others to help. Commented Dec 9, 2021 at 2:29
  • Alright, thank you for the tip! Commented Dec 9, 2021 at 7:12

1 Answer 1

1

You need to compare the user provided birthdate with the dates in your rows variable. You can do this by converting your dates from your CSV file to datetime objects. You can then let Python do the heavy lifting and calculate if a date is between the two star sign dates. More information on datetime in Python can be found here. I've provided an example of how this can be done using your format of CSV file.

Code

import csv
from datetime import datetime

def main():
    # Read CSV file
    fields = []
    rows = []
    with open('TableHoroscope.csv', 'r') as csvfile:
        csvreader = csv.reader(csvfile)
        fields = next(csvreader)
        for row in csvreader:
            rows.append([row[0], datetime.strptime(row[1], " %d-%b"), datetime.strptime(row[2], " %d-%b")])

    # Calculate zodiac
    print_zodiac(user_birthday(), rows)

def user_birthday():
    date = input("Enter day: ")
    month = input("Enter month (e.g. Jan): ")
    print("Birthday: " + date + "-" + month)

    datetime_birthday = datetime.strptime(date + month, "%d%b")
    return datetime_birthday

def print_zodiac(birth_date, star_signs):
    # Compare birth date with zodiac signs
    for star_sign in star_signs:
        if (star_sign[1] <= birth_date and star_sign[2] >= birth_date):
            print("Your star sign is " + star_sign[0])

main()

Ouput

Enter day: 30
Enter month (e.g. Jan): Mar
Birthday: 30-Mar
Your star sign is Aries
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.