r/learnpython • • Oct 13 '24

My if statement doesnt work

Can anyone find any errors here? Im new to python so I dont know what I did wrong as every answer is responded with “it has 30 days”

month = input("Enter a month: ") month = month.upper() print(month) if month == "SEPTEMBER"or"APRIL"or"JUNE"or"NOVEMBER": print("it has 30 days") elif month == "JANUARY"or"MARCH"or"MAY"or"JULY"or"DECEMBER"or"AUGUST": print("it has 31 days") elif month == "FEBRUARY": print("it has 28 days, but 29 on a leap year") else: print("That's not a month")

This is what the output is like Enter a month: a A It has 30 days

1 Upvotes

18 comments sorted by

View all comments

Show parent comments

0

u/FoolsSeldom Oct 13 '24

Not about performance so much as most appropriate data structures for beginners to learn.

EDIT: However, u/nekokattt has provided an excellent breakdown of the pros-and-cons of the options in this case.

2

u/pm4tt_ Oct 13 '24

You talked about "benefits" between tuples and lists, and I told you in practice there are none in this specific case. Neither for one nor other.

1

u/FoolsSeldom Oct 13 '24

I asked, and you focused on performance. Thank you for your sharing that information.

As I said, u/nekokattt provided more information that provided a broader perspective that I found very beneficial.

I would have gone for a list as well but have previously been advised I should have used a tuple so was interested in alternative views.

1

u/nekokattt Oct 13 '24

Still worth noting a set that is hidden as a container is really the best thing to be using here. The set implies it is just a "bag" of unique things and hiding behind a Container as the interface means you're hiding how it actually works in practise.

That way, you can change it later and be happy knowing nothing will catch fire.

1

u/FoolsSeldom Oct 13 '24

Good point. It would be easy to jump to the assumption that order would be important because this is data that we often use in such a context.

I also would have usually used a list but have been challenged previously on not using a tuple so was interested on hearing alternative views without poisoning the well.

I don't use set as often as I probably should, and there are a lot of benefits to it.