Elif and else statements are not working

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:

from telnetlib import Telnet

import paramiko

import time

import getpass

ip_addr = input (r’Enter IP Address: ')

usr = input (r’Username: ')

pwd = getpass.getpass()

port = input(r’Enter Port Number: ')

oper = input(r’Disable (D) or Enable (E): ')

def act_telnet (ip_addr, usr, pwd, port, oper):

tn = Telnet(ip_addr)

tn.read_until(b'Username: ')

tn.write(usr.encode('ascii') + b'\n')

if pwd:

    tn.read_until(b'Password: ')

    tn.write(pwd.encode('ascii') + b'\n')


tn.write(b'conf t\n')


tn.write(f'interface gi0/{port}\n'.encode('UTF-8'))


if oper == 'D' or 'd':



    print(f'{ip_addr} : Disabled Gi0/{port}.')

elif oper == 'E' or 'e':

    tn.write(b'no shutdown\n')


    print(f'{ip_addr} : Enabled Gi0/{port}.')


    print('Wrong Input! Try Again.')


act_telnet(ip_addr, usr, pwd, port, oper)

Please check!!

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…

if oper in ('D', 'd'):

Hope that helps!

1 Like

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…

Thank you so much for your help…

This line is wrong:

if oper == 'D' or 'd':

That line always matches, and so the elif and else lines will never
run. That line means:

if (oper equals 'D'), or if ('d' is a true value):

but ‘d’ is a true value, so it will always match.

You can change it to one of these:

if oper == 'D' or oper == 'd':

if oper.lower() == 'd':

if oper in ('D', 'd'):

and the same applies for the ‘E’ test later on.

Hello, @nayan12321, and welcome to the Python Forum!

This chart is very useful to programmers for constructing expressions and for calculating the values produced by them:

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'

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.