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

4

u/ste_wilko Oct 13 '24

A tidier way would be to use lists and the in() operator

longMonth = ["JANUARY", "MARCH", "MAY", "JULY", "AUGUST", "OCTOBER", "DECEMBER"]
shortMonth = ["APRIL", "JUNE", "SEPTEMBER", "NOVEMBER"]

month = input("Enter a month: ").upper()

if month in longMonth:
    print(f"{month} has 31 days")
elif month in shortMonth:
    print(f"{month} has 30days")
elif month == "FEBRUARY":
    print(f"{month} has 28 days (and 29 days in a leap year)")
else:
    print(f"{month} is not a month")

1

u/FoolsSeldom Oct 13 '24

Why lists rather than tuples? Don't see a benefit.

4

u/nekokattt Oct 13 '24

Sets are the most appropriate thing though, not tuples or lists. Use of either implies that order is somehow important, and that the ability to hold duplicate values is somehow important.

As for the argument of lists versus tuples, it doesn't make any difference unless you are explicitly trying to mutate them, at which point that is a problem with your code, not the collections themselves. Using one datatype over another just because it helps you to write less dodgy code to me is more of an issue with how you are writing the code than the datatype itself, which has specific use cases.

For beginners, this can also confuse them into thinking tuples make everything readonly when they don't, they only apply on a shallow level.

Lists are "academically" more correct here than tuples because tuples are designed to be a holder of bits of data that relate to a single entity, and their size is expected to be consistent as a result. Lists are designed as a way of representing multiple unrelated things, such as possible values in a domain of elements. You can tell that this is the case by how typing.Tuple differs to typing.List.

The concept of lists versus tuples really roots itself in things like database theory. Your table would be a list of records. Your records would be tuples. That is also why namedtuple exists in Python in the way it does.

Abusing the mutability and immutability aspects IMO is not fantastic unless you already have very haphazard code. I don't think it is a solution to that either. It may prevent beginners shooting themselves in the foot initially but I don't feel like it lends itself to writing consistent code further down the line.

In a production codebase, I'd much rather see...

SHORT_MONTHS: Sequence[str] = ["APRIL", "JUNE", ...]

...rather than...

SHORT_MONTHS = ("APRIL", "JUNE", ...)

...purely because it shows intent more clearly. If I see a list, I know that I cannot make assumptions about the size of it before runtime. With a tuple, the size is expected to match the things stored in it, and changing the size results in the meaning being different (for example, destructuring).

1

u/FoolsSeldom Oct 13 '24

Great advice. Thank you.