Python script if-else statement doubt

0 votes

Can you please explain when I give input as 1, why I am not able to see any results for below code?

N = int(input("enter number:"))
if N%2 != 0:
    if N >=2 and N<=5:
        print (N, "Not Wired")
    elif N>=6 and N<=20:
        print(N," ->Wired")
    elif N>20:
        print(N," ->NotWired")
   else:
        print ("Wired")
May 23 in Python by Sanjeev
9 views

1 answer to this question.

0 votes

The reason for this is that the else loop is inside the 2nd if loop, due to which the condition for when the input is 1 is not getting checked. This code should work fine:

N = int(input("enter number:"))
if N%2 != 0:
    if N >=2 and N<=5:
        print (N, "Not Wired")
    elif N>=6 and N<=20:
        print(N," ->Wired")
    elif N>20:
        print(N," ->NotWired")
else:
    print ("Wired")
answered May 23 by Rajan

Related Questions In Python

0 votes
1 answer

how to exit a python script in an if statement

This works fine for me: while True: ...READ MORE

answered Sep 19, 2018 in Python by bug_seeker
• 15,310 points
749 views
+1 vote
3 answers
0 votes
1 answer

Replacements for switch statement in Python?

You could use a dictionary: def f(x): ...READ MORE

answered May 29, 2018 in Python by ariaholic
• 7,320 points
20 views
0 votes
1 answer

What is the Python equivalent for a case/switch statement?

if x == 'a':  # Do the ...READ MORE

answered Jul 26, 2018 in Python by Priyaj
• 56,160 points
69 views
0 votes
1 answer

How to use nested if else statement in python?

The working of nested if-else is similar ...READ MORE

answered Nov 19, 2018 in Python by Nabarupa
34 views
0 votes
1 answer

How to write inline if statement for print in Python?

Inline if-else expression must always contain the else ...READ MORE

answered Dec 4, 2018 in Python by Nymeria
• 3,500 points

edited Dec 6, 2018 by Nymeria 633 views
0 votes
1 answer

what is an else if statement in python?

if condition:    statement elif condition:     statement #this is how you use ...READ MORE

answered Mar 15 in Python by Waseem
• 3,400 points
28 views
0 votes
1 answer

what are nested if statements in python?

nested if statements are statements with more ...READ MORE

answered May 20 in Python by Mohammad
• 1,500 points
13 views
0 votes
1 answer

Python: Doubt regarding private access specifier

In Python, you don't write to other ...READ MORE

answered May 23 in Python by Jagan
10 views