2015-04-22 134 views
0
sales = 1000 

#def commissionRate(): 

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

if if (sales <= 10000 and >= 15000): line上的语法错误。特别是在等号。为什么我得到语法错误?

+0

是您的实际缩进? –

回答

6

你需要比较sales对第二个条件也:

In [326]: 

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

你需要这个:

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

另外,你不需要括号周围if条件()

if sales <= 10000 and sales >= 15000: 

正常工作

您可以将其改写为更加紧凑:

In [328]: 

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

所以if 10000 <= sales <= 15000:作品也感谢@Donkey香港

另外(感谢@pjz)和无关的代码是逻辑上的销售不能都小于10000并且大于15000.

因此即使没有语法错误,条件也不会是True

你想if sales > 10000 and sales <= 15000:if 10000 <= sales <= 15000:这可能更清楚你

只是对if 10000 <= sales <= 15000:语法扩展(感谢@will您的建议),在蟒蛇一个可以执行数学比较lower_limit < x < upper_limit还解释here是不是更自然通常是if x > lower_limit and x < upper_limit:

这样的比较来进行链接,从文档:

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.

+4

值得一提的是,如果(10000 <=销售<= 15000)'也许? – miradulo

+0

@DonkeyKong好点,也编辑了这个例子 – EdChum

+1

你也应该指出他的逻辑错误:销售永远不能小于10k和大于15k。 – pjz

2

关于语法:

if (sales <= 10000 and >= 15000):应该if (sales <= 10000 and sales >= 15000):

关于逻辑:

销售情况从来没有samller临屋区n或等于10,000且大于或等于15,000

if (10000 <= sales <= 15000):

相关问题