Posts: 6
Threads: 3
Joined: Mar 2023
Mar-28-2023, 08:00 AM
Greetings, Python community!
I hope you are doing well.
I recently started re-learning Python (I love it too much!). I'm in the lesson about conditionals, and I've come across something that has left me a little confused. I'm practicing with a code snippet that sends a message depending on user input; my question is about the elif conditional. The program works well if the input is greater than or equal to the LIMIT NUMBER or if the input is less than the LIMIT NUMBER . The problem starts when the input is equal to 12; my intention is to display a message indicating that the number 12 was entered. However, for some reason, when this number is entered, the interpreter skips this statement and outputs the message equivalent to the input being greater than or equal to the LIMIT NUMBER .
The code in question is shown below:
LIMIT_NUMBER: int = 10
user_input = int(input("Please, enter a number: "))
if user_input >= LIMIT_NUMBER:
print(f"Your number is greater or equal to {LIMIT_NUMBER}.")
elif user_input == 12:
# The line above is the product of my doubt.
# Why elif is ignored even if user_input == 12?
print("This line will be ignored by the interpreter.")
else:
print(f"Your number is smaller than {LIMIT_NUMBER}.") Assuming that the number 12 was entered:
Output: Your number is greater or equal to 10.
The interpreter is omitting line 6, completely ignoring the elif statement even though the condition is met. I am quite puzzled. Is there something that I am ignoring or doing wrong? I have already done some research on several sites, but I can't find an answer to my question.
I think that would be my main question for the moment; I look forward to any answer. Thank you very much in advance, comrades!
- Carmazum
Posts: 4,789
Threads: 76
Joined: Jan 2018
Mar-28-2023, 08:32 AM
(This post was last modified: Mar-28-2023, 08:33 AM by Gribouillis.)
In a sequence such as
if condition1:
...
elif condition2:
...
elif condition3:
...
else:
... Only the code corresponding to the first condition that evaluates to True is executed. The code in the other branches are not executed. In your case, the user_input >= LIMIT_NUMBER condition evaluates to True when the user inputs 12.
Posts: 6
Threads: 3
Joined: Mar 2023
Mar-29-2023, 01:43 AM
(Mar-28-2023, 08:32 AM)Gribouillis Wrote: In a sequence such as
if condition1:
...
elif condition2:
...
elif condition3:
...
else:
... Only the code corresponding to the first condition that evaluates to True is executed. The code in the other branches are not executed. In your case, the user_input >= LIMIT_NUMBER condition evaluates to True when the user inputs 12.
Oh, I get it now! Python only interprets the first statement it detects as True . The others, whether True or False , will be ignored. I now have a better understanding of the interpreter's execution flow, thank you very much Gribouillis; you have saved me!
Posts: 8,160
Threads: 160
Joined: Sep 2016
Mar-29-2023, 06:01 AM
(This post was last modified: Mar-29-2023, 06:02 AM by buran.)
(Mar-29-2023, 01:43 AM)Carmazum Wrote: Python only interprets the first statement it detects as True. To be precise, it works top down until (and if) it reach a if / elif that is evaluated as True or eventually (if present) else . That may have effect, because some code may be executed even if the condition as whole is False . In other words, code may be executed while evaluating the condition, even if the result is False and the respective code block under that condition is not executed.
Simplified example
def spam():
print('Inside spam') # just to show that function has been called
return False
if spam():
print('spam is True')
else:
print('spam is False') output
Output: Inside spam
spam is False
Posts: 6
Threads: 3
Joined: Mar 2023
(Mar-29-2023, 06:01 AM)buran Wrote: (Mar-29-2023, 01:43 AM)Carmazum Wrote: Python only interprets the first statement it detects as True. To be precise, it works top down until (and if) it reach a if /elif that is evaluated as True or eventually (if present) else . That may have effect, because some code may be executed even if the condition as whole is False . In other words, code may be executed while evaluating the condition, even if the result is False and the respective code block under that condition is not executed.
Simplified example
def spam():
print('Inside spam') # just to show that function has been called
return False
if spam():
print('spam is True')
else:
print('spam is False') output
Output: Inside spam
spam is False
Oh, so, Python does interpret the code it finds even if it is False . The detail is that, if it is False , it will not execute the code inside the conditional.
Did I understand correctly? Sorry if I didn't, I haven't programmed anything for a while.
Also, sorry for the late reply! (>_<)
Posts: 8,160
Threads: 160
Joined: Sep 2016
Mar-31-2023, 05:25 AM
(This post was last modified: Mar-31-2023, 05:26 AM by buran.)
(Mar-30-2023, 09:18 PM)Carmazum Wrote: Oh, so, Python does interpret the code it finds even if it is False. The detail is that, if it is False, it will not execute the code inside the conditional. I think we say the same, but just in case there is still some misunderstanding. It does not magically know if condition is True or False. It evaluates the condition to determine whether it is True or False. And it evaluates conditions one by one, top down.
Posts: 6
Threads: 3
Joined: Mar 2023
Apr-01-2023, 12:01 AM
(Mar-31-2023, 05:25 AM)buran Wrote: (Mar-30-2023, 09:18 PM)Carmazum Wrote: Oh, so, Python does interpret the code it finds even if it is False. The detail is that, if it is False, it will not execute the code inside the conditional. I think we say the same, but just in case there is still some misunderstanding. It does not magically know if condition is True or False. It evaluates the condition to determine whether it is True or False. And it evaluates conditions one by one, top down.
Ah, ok, it's super clear to me now! I apologize for repeating the information, buran; what happens is that it works for me to see if I understood about the subject. ( ̄▽ ̄*)ゞ Thank you very much for helping me with your knowledge, buran!
...and sorry again for repeating the information. (>_<)
|