Here you go:
if number > 10 and number < 20 then:
print "Number between 10 and 20"
Each comparison is an independent expression, combined together with and
, or
, etc.
Question
I'm writing a small program that requires a few if statements like so:
number=x/(y*y)
if number <5:
print "blahblahblah"
if number >5 or <10:
print "blahblahblah"
if number >10.5 or >15.0:
print "blahblahblah"
if number >15:
print "blahblahblah"
Basically I'm having trouble with using both > and < in my if statement and can't work out how to say "if less than 4 or more than 5"
When I load the program I get the following error -
Invalid syntax Your code contains at least one syntax error, meaning it is not legal jython.
Any help would be greatly appreciated
La solution
Here you go:
if number > 10 and number < 20 then:
print "Number between 10 and 20"
Each comparison is an independent expression, combined together with and
, or
, etc.
Autres conseils
As an addition to Paul answer with or
, and
etc you can use if 5 < number < 10:
:
if number < 5:
print("a")
if 5 < number < 10:
print("b")
if 10 < number < 15:
print("c")
if number > 15:
print("d")
You can also use elif
(short of else if
) and else
which I prefer:
if number < 5:
print("a")
elif number < 10:
print("b")
elif number < 15:
print("c")
else:
print("d")
BTW in your code you used or
instead of and
.