-1

The problem is like this

My solution:

n= int(input())
if n%2==0:
    if (n >=2 & n<=5):
        print("Not Weird")
    elif n >=6 & n<=20:
        print("Weird")
    else:
        print ("Not Weired")
else:
    print ("Weird")

Now, if I enter 18 then it should be printed "Weird". But it is showing "Not Weird".

I am not getting where the problem of my code is.

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
Wasema007
  • 3
  • 2
  • Does this answer your question? [Python bitand (&) vs and](https://stackoverflow.com/questions/15251121/python-bitand-vs-and) – LinkBerest Jul 13 '20 at 03:04

3 Answers3

1

The operator & is the bitwise and operation. You need logical AND, instead, and it is done by means of and keyword.

Just replace & with and:

n= int(input())
if n%2==0:
    if (n >=2 and n<=5):    #modified here!
        print("Not Weird")
    elif n >=6 and n<=20:   #modified here!
        print("Weird")
    else:
        print ("Not Weird")
else:
    print ("Weird")
Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
1

try using:

if n >= 2 and n <= 5:
elif n >= 6 and n <= 20:

instead of binary and

PirklW
  • 484
  • 2
  • 16
0

The problem is that you are using the bitwise and & instead of the logical operator and.

n= int(input())

if n%2 == 0:
    if n >=2 and n<=5:   # Changed condition
        print("Not Weird")
    elif n >=6 and n<=20:   # Changed condition
        print("Weird")
    else:
        print ("Not Weired")

else:
    print ("Weird")
AJ_
  • 1,455
  • 8
  • 10