+ 1

Please see this code it's troubling me - this code isn't working. Whenever I type any month it implements the first if line only

print("Please put the month in the suggested manner -- " "EX - January or january and so on...") month = input("Enter The Month in alphabetical format : ") if month == 'January' or 'january': print('There are 31 Days in',month) elif month == 'February' or 'february': print('There are 28 days in a simple year & 29 days in a leap year in ',month) elif month == 'March' or 'march': print('There are 31 Days in ',month) elif month == 'April' or 'april': print('There are 30 Days in ', month) elif month == 'May' or 'may': print('There are 31 Days in ',month) elif month == 'June' or 'june': print('There are 30 Days in ',month) elif month == 'July' or 'july': print('There are 31 Days in ',month) elif month == 'August' or 'august': print('There are 31 Days in ',month) elif month == 'September' or 'september': print('There are 30 Days in ',month) elif month == 'October' or 'october': print('There are 31 Days in ',month)

29th Mar 2022, 7:00 AM
Dikshant Sharma
Dikshant Sharma - avatar
7 odpowiedzi
+ 2
Dikshant Sharma there is a string.capitalize() method that does what you want. It ensures the first letter is capitalized, and all the rest are lowercase. The code could be reduced by comparing groups of months having the same number of days. print("Please put the month in the suggested manner -- EX - January or january and so on...") month = input("Enter the month in alphabetical format: ").capitalize() if month in ('January', 'March', 'May', 'July', 'August', 'October', 'December'): print('There are 31 Days in', month) elif month in ('April', 'June', 'September', 'November'): print('There are 30 Days in ', month) elif month == 'February': print('There are 28 days in a simple year & 29 days in a leap year in ', month)
29th Mar 2022, 8:42 AM
Brian
Brian - avatar
+ 4
You can't simply compare string using logical OR like that. Try like this ... if month.lower() == 'january': elif month.lower() == 'february': etc etc ... This way we check for month name in lowercase against the literals 'january', 'february' etc.
29th Mar 2022, 7:22 AM
Ipang
+ 2
Thankssss a lot Ipang for your help!! 👍🏻👍🏻
29th Mar 2022, 8:34 AM
Dikshant Sharma
Dikshant Sharma - avatar
+ 2
# I hope this link helps you https://code.sololearn.com/czaFVUg7xxXF import calendar def number_of_days_in_month(mIdx): return calendar.monthrange(2022, mIdx)[1] d31 = []; d30 = [] for month_idx in range(1, 13): if number_of_days_in_month(month_idx)==31: d31.append(calendar.month_name[month_idx]); d31.append(calendar.month_abbr[month_idx]) elif (number_of_days_in_month(month_idx)==30): d30.append(calendar.month_name[month_idx]); d30.append(calendar.month_abbr[month_idx]) print("Please put the month in the suggested manner -- EX - February or Feb and so on...\n") month = input("Enter The Month in alphabetical format : ") m = str.title(month).strip() ;print (m) if m in d31: print('There are 31 Days in', m) elif m in d30: print('There are 30 Days in', m) elif m=='February' or m=="Feb": print('There are 28 days in a simple year & 29 days in a leap year in', m) else: print("Incorrect month")
29th Mar 2022, 8:41 AM
SoloProg
SoloProg - avatar
+ 1
Basically I have made this code on which it shows how many days are there in any month which you input. Suppose if you input August so it should show 31 days in August or if you input February so it should show 28 days normally and 29 days in leap year and so onn.... But whenever I am inputting any month this code only implements the first if line which is written of January and any month I input it will show "There are 31 days in (whatever month I have put) " This same line repeates everytime. Hope you would have understood if not then ask me. But please somebody help me to resolve this code!! Thanks!
29th Mar 2022, 7:04 AM
Dikshant Sharma
Dikshant Sharma - avatar
+ 1
Thanks a lot Brian.... It helped me!! 👍🏻
29th Mar 2022, 11:18 AM
Dikshant Sharma
Dikshant Sharma - avatar
+ 1
Thanks SoloProg
29th Mar 2022, 11:18 AM
Dikshant Sharma
Dikshant Sharma - avatar