I am writing a code. The code is getting executed without any error…but it is not executing the elif and else statements…I googled, but not finding any solution…any help please…below is the code:
You’ve misunderstood Python’s order of operations. This…
if oper == 'D' or 'd':
Means literally “if the value of oper is ‘D’ OR if the string ‘d’ is
nonempty” which I’m sure is not what you intended. What you probably
want instead is…
if oper == 'D' or oper == 'd':
But you’ll often find this more concisely written as…
Thank you so much for your help @fungi .
if oper in (‘D’, ‘d’): # gave me output: 'Wrong Input! Try Again.
but if oper == ‘D’ or oper == ‘d’: # worked…
When expressions are evaluated by the Python interpreter, the operations are performed in order from highest precedence to lowest. The chart lists operators from highest precedence at the top to lowest at the bottom. Some of the operators in the chart can be clicked to reveal information on what they do. This applies to the or operator. Others can be looked up via a search.
The chart reveals that the == operator precedes the or operator in the chart. That observation and the information available regarding or can help you understand what happens when this line is executed:
if oper == 'D' or 'd':
Using the chart can also help with learning what operators exist, which will prove useful for composing expressions for a program.
Let’s check that expression in an interactive Python interpreter.
>>> oper = input(r'Disable (D) or Enable (E): ')
Disable (D) or Enable (E): E
>>> oper == 'D' or 'd'
'd'
>>>
E was entered. The expression then evaluated to 'd'. You were probably hoping for False. In fact, 'd', from a boolean perspective, functions as if it were True.
Please see if you can use the chart and associated information to understand what happened, and how to fix it.