Hello, As a beginner into python/coding as a whole, there are little things that happen that I quite don’t understand.
I have this Code that has you put in a number 1-12 depending on what month your birthdate is (1 for january, 2 for February, etc)
Whenever you put this number in, it will print out that respecively month and have a short little description of it. However, the problem I am having is my else command is printing out something when it is not supposed to. It is supposed to print (“Not a valid month”) if you put a number that does not have a month attached to it… however it is printing that no matter what as well as whatever month. Here is what I have
Month = int(input("Which Number Month is your birthday? "))
if Month == 1:
print(“January! A cold and dry month”)
if Month == 2:
print(“Februrary! A Month filled with love!”)
if Month == 3:
print(“March, Go Green during this month!”)
if Month == 4:
print(“April, This month is when the air begins to get warm!”)
if Month == 5:
print("May, Wrapping up school at last! ")
if Month == 6:
print("June, Go out and get some sun! ")
if Month == 7:
print("July, Time to watch some fireworks! ")
if Month == 8:
print("August, The best month of course! ")
if Month == 9:
print("September, Starting to get windy! ")
if Month == 10:
print("October… Spooky Season! ")
if Month == 11:
print(“November, Time to settle down and get indoors”)
if Month == 12:
print("December, Brrrr, Santa Claus is near! ")
Essentially what steven is saying is you can’t use if statements back to back for the same variable. You can use them back to back for different variables but not the same. You need to use if > elif > else.
You can have infinite elif statements.
While it must of course be finite, it also cannot be arbitrarily many elif statements. On my machine, the limit is somewhere between 2900 and 3000 elifs. Too many, and you get
RecursionError: maximum recursion depth exceeded during compilation
Ani, I think @steven.rumbalski gave a more focused answer about how the code does not work. The flow of control using individual if statements works by testing each and moving on to test the next and so on. The lone else statement is bound to only the last if.
So when you look at the code, for numbers like 1 and 2 till 11, all the if statements but one result in a False and the contents are skipped. The last if is special because the “then” part is skipped so the “else” part always fires. It is the only one with an “else” clause so any argument other than 1-12 will also hit that else.
A better design here, and there are many, is the straightforward series of if/elif/…/elif/else which propagates the testing of the same variable and quits as soon as one matches and the code in it is run, and only upon all failing is the *else reached and executed.
But for the problem being discussed, there are actually many interesting ways to do it with easier to understand code. It depends on how far along in python education you have reached or can use.
As an example, consider this design.
Create a data structure that contains the names of the twelve months (in English) exactly as the sentences you want so that you can say MyMonths[2] and get back “February! A Month filled with love!”.
Use a single if statement that tests if the Month number requested is between 1 and 12 and if it does, print the result. I mean print MyMonth[Month] of course.
A single else below that handles any other case.
My understand of what @Anixtzer says, I am not clear if they are right.
Every if statement can examine any variable it wants. There are no limits.