0
sales = 1000

#def commissionRate():  

if (sales < 10000):
    print("da")  
else:
    if (sales <= 10000 and >= 15000):
        print("ea")

Syntax error on the if (sales <= 10000 and >= 15000): line. Particularly on the equal signs.

1
  • Is that your actual indentation? Commented Apr 22, 2015 at 15:39

2 Answers 2

6

You need to compare sales against the second condition also:

In [326]:

sales = 1000
​
#def commissionRate():
​
​
if (sales < 10000):
    print("da")
else:
    if (sales <= 10000 and sales >= 15000):
        print("ea")
da

you need this:

if (sales <= 10000 and sales >= 15000):
                       ^^^^ sales here

Additionally you don't need parentheses () around the if conditions:

if sales <= 10000 and sales >= 15000:

works fine

You could rewrite it to the more compact:

In [328]:

sales = 1000
​
if sales < 10000:
    print("da")
else:
    if 10000 <= sales <= 15000:
        print("ea")
da

so if 10000 <= sales <= 15000: works also, thanks @Donkey Kong

Additionally (thanks @pjz) and nothing to do with code is that logically sales cannot be both less than 10000 and greater than 15000.

So even without the syntax errors that condition will never be True.

You wanted if sales > 10000 and sales <= 15000: or if 10000 <= sales <= 15000: which maybe clearer for you

Just to expand on the if 10000 <= sales <= 15000: syntax (thanks @will for the suggestion), in python one can perform math comparisons lower_limit < x < upper_limit also explained here that are more natural than the usual if x > lower_limit and x < upper_limit:.

This allows comparisons to be chained, from the docs:

Formally, if a, b, c, ..., y, z are expressions and op1, op2, ..., opN are comparison operators, then a op1 b op2 c ... y opN z is equivalent to a op1 b and b op2 c and ... y opN z, except that each expression is evaluated at most once.

Sign up to request clarification or add additional context in comments.

4 Comments

Worth mentioning if (10000<= sales <= 15000) too maybe?
You should also point out his logic error: sales can never be both less than 10k and greater than 15k.
Could also mention how the lower < x < upper works by expanding it into a chain of comparisons, which means you can do other stuff too which can allow some other stuff you wouldn't normally see in standard maths comparisons.
@will good point, have expanded this and added a link to the official docs, thanks
2

About syntax:

if (sales <= 10000 and >= 15000): should be if (sales <= 10000 and sales >= 15000):

About logic:

sales can never samller than or equal to 10,000 and bigger than or equal to 15,000

if (10000 <= sales <= 15000):

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.