-1

I am trying to set the variable numberOfDays equal to the number of days in the month. However, the elif statement has a flaw. I am clearly not using the "or" statement correctly because because when I enter anything, it always says that numberOfDays is equal to 30.

   monthSelection = input("Enter the month you wish to view: ")

   if monthSelection == "February":
        numberOfDays = 28
   elif monthSelection == "April" or "June" or "September" or "November":
        numberOfDays = 30
   else:
        numberOfDays = 31

Is there any way to reformat this code to make it functional?

Allie M
  • 61
  • 1
  • 5
  • 1
    change to `elif monthSelection in ("April", "June", "September", "November"):` – eyllanesc Dec 10 '18 at 23:51
  • 1
    It's not a statement, it's a logical connective; it goes between two things that can be true-ish or false-y. `a == b or c` means "a equals b, or c is truth-y". – molbdnilo Dec 10 '18 at 23:56
  • Except that `"September"` and `"November"` are probably never evaluated if `"June"` evaluates to `true`. – Maarten Bodewes Dec 11 '18 at 00:01

1 Answers1

-1

Use in rather than or:

if monthSelection == "February":
    numberOfDays = 28
elif monthSelection in ("April", "June", "September", "November"):
    numberOfDays = 30
else:
    numberOfDays = 31

Otherwise, you need to specify each equality separately:

if monthSelection == "February":
    numberOfDays = 28
elif monthSelection == "April" or monthSelection == "June" or monthSelection == "September" or monthSelection == "November":
    numberOfDays = 30
else:
    numberOfDays = 31
Tim
  • 2,756
  • 1
  • 15
  • 31