python - Why do I get a syntax error? -
sales = 1000 #def commissionrate(): if (sales < 10000): print("da") else: if (sales <= 10000 , >= 15000): print("ea") syntax error on if (sales <= 10000 , >= 15000): line. particularly on equal signs.
you need compare sales against second condition also:
in [326]: sales = 1000 #def commissionrate(): if (sales < 10000): print("da") else: if (sales <= 10000 , sales >= 15000): print("ea") da you need this:
if (sales <= 10000 , sales >= 15000): ^^^^ sales here additionally don't need parentheses () around if conditions:
if sales <= 10000 , sales >= 15000: works fine
you rewrite 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, @donkey kong
additionally (thanks @pjz) , nothing code logically sales cannot both less 10000 , greater 15000.
so without syntax errors condition never true.
you wanted if sales > 10000 , sales <= 15000: or if 10000 <= sales <= 15000: maybe clearer you
just expand on if 10000 <= sales <= 15000: syntax (thanks @will suggestion), in python 1 can perform math comparisons lower_limit < x < upper_limit explained here more natural usual if x > lower_limit , x < upper_limit:.
this allows comparisons chained, docs:
formally, if
a,b,c, ...,y,zexpressions ,op1,op2, ...,opncomparison operators,a op1 b op2 c ... y opn zequivalenta op1 b , b op2 c , ... y opn z, except each expression evaluated @ once.
Comments
Post a Comment