# Program Will Quit With an "else" Statment

Asked by At

I am trying to make a Python Script that will tell you what day you were born on based off of your DOB.

But I needed to make it so if you were born on either the months of "February","April","June","September","November" it would need to have a different equation, so for now I made it so if you put your date as "6" (June) it would just

``````print("This Is True")
``````

So I arranged it like this

``````if M == '2':
i = 30
X = 'Answer'

if M == '4':
i = 30
X = 'Answer'

if M == '6':
i = 30
X = 'Answer'

if M == '9':
i = 30
X = 'Answer'

if M == '11':
i = 30
X = 'Answer'

os.system('CLS')

if X == 'Answer':
print('This Is True')
input()

elif X != 'Answer':
print('This is False')
input()
``````

yet when It is not one of the Months listed above it will

``````print('This is False')
``````

yet when I enter something like "10" (October) the program will shut and not display "This is False"

I know I have asked a lot today but once again (if you couldn't tell by the code) I am VERY new

If anyone would also like to point out any tips on where the code could potentially be better here is all of it:

``````import os

M = 'Monday'
T = 'Tuesday'
W = 'Wednesday'
TT = 'Thursday'
F = 'Friday'
S = 'Saturday'
SS = 'Sunday'

print('Enter Your DOB as DD/MM/YYYY: ')

D = input("Day: ")

M = input("Month: ")

if M == '2':
i = 30
X = 'Answer'

if M == '4':
i = 30
X = 'Answer'

if M == '6':
i = 30
X = 'Answer'

if M == '9':
i = 30
X = 'Answer'

if M == '11':
i = 30
X = 'Answer'

Y = input("Year: ")
if (len(Y)) < 4:
print('You Have Entered A Too Short Or Too Long of a Year')
input()
os._exit(0)

os.system('CLS')

if X == 'Answer':
print('This Is True')

elif X != 'Answer':
print('This is False')
input()

os.system('CLS')

print("You Were Born On A:")

input("Press ENTER To Exit")
``````

## 3 Answers On Best Solutions

Perhaps this might be too advanced for you, but we have something called a datetime module which is meant for such things, so your code can be much shortened as

``````import os
import datetime

#Get date in correct format from user
dob = input('Enter Your DOB as DD/MM/YYYY:')

#Convert to datetime object
date_obj = datetime.datetime.strptime(dob, '%d/%m/%Y')

#Get day of week
print("You Were Born On A {}".format(date_obj.strftime("%A")))
``````

The output will look like

``````Enter Your DOB as DD/MM/YYYY:31/01/1991
You Were Born On A Thursday
``````

In addition, some more ways you can improve your previous code.

1. Use a list to save all your `weekdays`
2. Instead of so many `if-else`, use a list like `['2' , '4', '6', '9', '11']` to save all your months, and use `in` to check if the month checks out, also if you are using `MM` you should check against `02,04,06...` etc
3. Use `sys.exit()` to exit your code

So an improvement on the code might look like.

``````import sys
import os

#List of weekdays
weekdays = ['Monday' ,'Tuesday' ,'Wednesday', 'Thursday' , 'Friday' ,'Saturday', 'Sunday']

print('Enter Your DOB as DD/MM/YYYY: ')

day = input("Day: ")
month = input("Month: ")

X = ''

#Check for month
if month in  ['02' , '04', '06', '09', '11']:
i = 30
X = 'Answer'

Y = input("Year: ")
if (len(Y)) < 4:
print('You Have Entered A Too Short Or Too Long of a Year')
sys.exit(0)

if X == 'Answer':
print('This Is True')

elif X != 'Answer':
print('This is False')

print("You Were Born On A:")

input("Press ENTER To Exit")
`````` On

Why you aren't using True / False

I have rewritten something and added the idea from @Max

``````import os

M = 'Monday'
T = 'Tuesday'
W = 'Wednesday'
TT = 'Thursday'
F = 'Friday'
S = 'Saturday'
SS = 'Sunday'

X = False

print('Enter Your DOB as DD/MM/YYYY: ')

D = input("Day: ")
M = input("Month: ")

if M in ['2', '4', '6', '9', '11']:
i = 30
X = True

Y = input("Year: ")
if (len(Y)) < 4:
print('You Have Entered A Too Short Or Too Long of a Year')
input()
os._exit(0)

os.system('CLS')

if X:
print('This Is True')
else:
print('This is False')
input()

os.system('CLS')

print("You Were Born On A:")
input("Press ENTER To Exit")
`````` On

Pay attention to some mistakes in your code:

• First you instantiate the `M` var to be `Monday` (`M = 'Monday'`), then you override it's value with an input from a user (`M = input("Month: ")`)

• You create the `X` var only when one of your `if` is set to `true`, meaning `M` is one of `2,4, 6, 9, 11)

You should create `X` outside the `if`

something like:

``````day = int(input("Day: "))
month = int(input("Month: "))

X = "Not Answer"
if M in (2, 4, 6, 9, 11):
i = 30
X = 'Answer'

...

if X == 'Answer':
print('This Is True')
else:
print('This is False')
input()
``````