Unexpected input value detection for dates in Zodiac calendar sample

Viewed 65

I'm doing a task in Python, which is to create a zodiac calendar judging by input month and day of the month.

I've made a successful calendar, however, I have no idea how to solve the problem, if I input an incorrect month name or nonexistent date.

I have tried tons of things: to create a different hierarchy, or under each zodiac sign I wrote else: print ('Entering an incorrect date'). I even tried to register with if day <0 and day> 31 or month! = 'March' or month! = 'April', etc... It turned out to be a huge useless function in the end, and nothing worked either.

I feel that there is a one-line solution here to detect days < 0 and > 31 and incorrect month name, but I just can't understand it. Could somebody help, please?

month = input('Enter the month: ')
day = int(input ('Enter the date of month: '))
if day >= 21 and day <= 31 and month.lower() == 'march' or day >=1 and day <= 19 and month.lower() == 'april':
    zodiac='Aries'
if day >= 20 and day <= 30 and month.lower() == 'april' or day >=1 and day <= 19 and month.lower() == 'may':
    zodiac='Taurus'
if day >= 21 and day <= 31 and month.lower() == 'may' or day >=1 and day <= 20 and month.lower() == 'june':
    zodiac='Gemini'
if day >= 21 and day <= 30 and month.lower() == 'june' or day >=1 and day <= 22 and month.lower() == 'july':
    zodiac='Cancer'
if day >= 23 and day <= 31 and month.lower() == 'july' or day >=1 and day <= 22 and month.lower() == 'august':
    zodiac='Leo'
if day >= 23 and day <= 31 and month.lower() == 'august' or day >=1 and day <= 22 and month.lower() == 'september':
    zodiac='Virgo'
if day >= 23 and day <= 30 and month.lower() == 'september' or day >=1 and day <= 22 and month.lower() == 'october':
    zodiac='Libra'
if day >= 23 and day <= 31 and month.lower() == 'october' or day >=1 and day <= 21 and month.lower() == 'november':
    zodiac='Scorpio'
if day >= 22 and day <= 30 and month.lower() == 'november' or day >=1 and day <= 21 and month.lower() == 'december':
    zodiac='Sagittarius'
if day >= 22 and day <= 31 and month.lower() == 'december' or day >=1 and day <= 19 and month.lower() == 'january':
    zodiac='Capricorn'
if day >= 20 and day <= 31 and month.lower() == 'january' or day >=1 and day <= 18 and month.lower() == 'february':
    zodiac='Aquarius'
if day >= 19 and day <= 28 and month.lower() == 'february' or day >=1 and day <= 20 and month.lower() == 'march':
    zodiac='Pisces'  
print('Conclusion:')
print(zodiac)
1 Answers

You might want to look into the datetime module. With it, you can do comparisons between dates and times easily without the need for extraneous if statements.

For example, if one were to do something like:

import datetime
dt = datetime.datetime.today()
print dt.month

Then you would get:

3

So instead of reading in input for months in a string format, you could simply ask for the number of the month to help create a datetime object more easily. Alternatively, you could do something like this:

month_input = input("What is the month?")
months = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"]
month_input = month_input.lower().strip() #convert to lowercase and remove whitespace
month_num = months.index(month_input) + 1 #Add one because lists start at index 0

That way you can get the number of the month to use a datetime object to do your comparisons and evaluations from there.

Related