0
Can 2 comparisions be clubbed together?
Say I want to club 2 comparisons like this 10<x+9<20 . Can I use it directly in python cause it's showing some concatenation error. https://code.sololearn.com/czhAbDISF5B2/?ref=app
3 Réponses
+ 5
Yes, by using logical operators, namely "and" as well as "or". Read up about them here:
https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2279/
For example,
10 < ( x + 9 ) < 20
would instead become
( x + 9 ) > 10 and ( x + 9 ) < 20
since both should be fulfilled.
+ 4
Yes, you can chain comparison operarors in python.
In your code you have to convert the input to number (int) first.
Then, if you have more than just a variable between the < < signs, you should put it in parentheses.
Fix:
a = int(input('Enter a number:'))
if 0 <= a < 10:
print('The number is between 0 (included) & 10')
elif 10 <= a < 20:
print('The number is between 10 (included) & 20')
else:
print('The number is greater than 20 (included)')
0
Say if I don't add a bracket having more than just a bracket between << signs, what goes wrong and why does it show concatenation error, Can someone explain it a bit elaborately. Mean to say how does python find it wrong when it tries to interpret it. Thanks for those who've helped me understand earlier.