Home > Software design >  Having A Problem With If Statements In Python
Having A Problem With If Statements In Python

Time:05-17

My goal is to have something like this:

x = 201
if x >= 100 <= 200: 
    print(x)
else:
    print('Less Than 100 Or More Than 200')

but it doesn't work:

output: 201

basically anything smaller than or equal 100 and anything smaller or equal 200

Would this be possible?

CodePudding user response:

Python has comparison chaining, written the same way as in mathematical notation:

if 100 <= x <= 200:

This is equivalent to:

if 100 <= x and x <= 200:

(Though if the expression x has side effects it will be evaluated twice in the second example but only once in the first.)

CodePudding user response:

you wanna do this :

x = 201
if  100<= x <= 200: 
    print(x)
else:
    print('Less Than 100 Or More Than 200')
  • Related