-2

My code runs fine but returns 31 days for all months if there is a spelling error. How can I correct this?

month= input ("Enter month :  ")
days = 31 

if month == "April" or month == "June" or month == "September" or month == "November" :
  days = 30 
elif month == "February" :      
  days = "28 or 29"

print(days)
Jakub
  • 489
  • 3
  • 13
Jeff Hust
  • 11
  • 1
  • What are you inputting? If I input `April`, I get `30`. – jkr Jul 30 '20 at 22:53
  • Your code is not entering any of the if statements – Ahmet Jul 30 '20 at 22:53
  • Are you inputting `April` or `april`? Try using `str.lower` and use lowercase in the code – Jab Jul 30 '20 at 22:54
  • 1
    Also instead of all those `or` statements. Why not use `if month in {"April", "June", "September", "November"}:` – Jab Jul 30 '20 at 22:57
  • Inputting exactly how months are supposed to....capital first letter small rest – Jeff Hust Jul 30 '20 at 23:03
  • 1
    Then the you're code running is not the code you posted. As the code posted works fine. – Jab Jul 30 '20 at 23:06
  • None of us have been able to reproduce your problem if we enter the month with an initial capital. – Barmar Jul 30 '20 at 23:17
  • On my end it is almost like it doesn't enter the if statements in my program – Jeff Hust Jul 30 '20 at 23:22
  • I realized what it was. When I input the month I put a space after the month and it was defaulting back to the 31. No space after month in input and it worked. Thanks to all for answering and trying to help. – Jeff Hust Jul 30 '20 at 23:27

2 Answers2

2

I suspect the error is how you are inputting the months. In your code, the input is case-sensitive. So you need to input April or March etc.

You can improve your code by sanitizing the input string (e.g., stripping whitespace and converting to lowercase) and by using the in statement.

month = input("Enter month :  ")
month = month.strip()  # strip whitespace on either end of the string.
month = month.lower()  # convert to lowercase.
days = 31

if month in {"april", "june", "september", "november"}:
    days = 30
elif month == "february":
    days = "28 or 29"
    
print(days)

In the code above, {"april", "june", "september", "november"} is a set. It is quick to look up whether values are in a set.

jkr
  • 17,119
  • 2
  • 42
  • 68
0

Use lowercase as suggested by jakub (coincidentally, the same name as mine) and you only need first three letters of month. This will help against spelling errors. For example, someone might misspell February as Febuary. If you also want to check for spelling errors in first three letters, you can use a while loop:

spelled_correctly = False
while not spelled_correctly:
    month = input('Enter month : ')
    month = month.lower()[:3]  # convert to lowercase
    if month in {'apr', 'jun', 'sep', 'nov'}:
        days = 30
        spelled_correctly = True
    elif month in {'jan', 'mar', 'may', 'jul', 'aug', 'oct', 'dec'}:
        days = 31
        spelled_correctly = True
    elif month == 'feb':
        days = '28 or 29'
        spelled_correctly = True
    elif month == '':
        days = 'Goodbye' # or whatever option you like
        spelled_correctly = True
    else:
        print('Month mispelled. Try again. Or press enter to quit.')
        spelled_correctly = False
print(days)
Jakub
  • 489
  • 3
  • 13