zahz
zahz

Reputation: 99

Finding a value in a range

def shazday(y):
    if y >= 1200 or <= 1299:
        x = 4

I keep getting syntax on the "=" just before 1299, any idea what I'm doing wrong? I'm trying to find any value between these two numbers to assign a variable. I've tried range itself as well but couldn't get it to work. If I could just find out how to properly check for a value in a range in Python, that would be great.

Upvotes: 0

Views: 114

Answers (3)

PhillipD
PhillipD

Reputation: 1817

def shazday(y):
    if y >= 1200 or y <= 1299:
        x = 4

Note the y before the second <=. While y is greater equal or smaller equal is valid in the English language and everybody understands the meaning, this does not work for programming languages...

What the interpreter does is something like

(y >= 1200) is it true or false?
(y <= 1299) is it true or false?

...and then applies the logical operator or. If the second y is missing, the compiler does not know what actually should be smaller equal 1299.

Edit:

Besides the missing y you may also change or to and. Otherwise, the condition will always be true because every number is >= 1200 or >=1299.

Upvotes: 1

CDspace
CDspace

Reputation: 2689

Look at it this way, in the if statement you want to evaluate several conditions. So we break it down

if y >= 1200 or <= 1299:

Is y >= 1200? Let's say yes.

if true or <= 1299

Next we ask is... wait a minute? There's nothing there to compare to! (blank) <= 1299 and the system has no idea what (blank) is supposed to be, so it yells at you until you give it something. In this case we have to tell it to check y

if y >= 1200 or y <= 1299:
                ^

Another way to form this is below, still satisfying that each comparison has something to compare to

if 1200 <= y <= 1299:

# this can be thought of as below
if 1200 <= y and
           y <= 1299:

Upvotes: 2

ecatmur
ecatmur

Reputation: 157414

if y >= 1200 or y <= 1299:

But you probably mean:

if 1200 <= y <= 1299:

Upvotes: 2

Related Questions