Issue
I am a very new Python student and I want to make some personal projects to learn more effectively. My question is why won’t this function work as expected for me? I tried to keep it as simple as possible but clearly I am missing something. Thanks for any help with this!
# This program will tell whether
# the inputted month contains 31
# days.
def month_checker(month):
if month == ['january','march','may','july','august','october']:
print('Hey this month has 31 days!')
month_checker('may')
Solution
You are comparing a str
with a list
which is why your code isn’t working. Like @Guimoute mentioned you should check whether the month
is in
the list of months.
Or, if you want, you can use the expanded if-elif-else
block which will be slightly more performant.
Solution 1:
def month_checker(month):
if month in ['january','march','may','july','august','october']:
print('Hey this month has 31 days!')
month_checker('may')
Solution 2:
def month_checker(month):
if month == 'january' or month =='march' or month == 'may' or month == 'july' or month == 'august' or month =='october':
print('Hey this month has 31 days!')
month_checker('may')
Answered By – kanuos
Answer Checked By – Senaida (BugsFixing Volunteer)