为什么这个elif语句给我一个错误?

时间:2019-01-25 06:53:53

标签: python and-operator

我不明白为什么我的elif语句出现错误。

我正在使用带有最新版本python的eclipse插件pydev

例如

if  sum(player_cards) = 14 and sum(computer_cards) = 10

这应该输出为True

elif sum(player_cards) > sum(computer_cards) and < 21 :

相反,它给了我这个:

  

“在第42行的第54列遇到” <“。期望出现以下情况之一:”(“ ...” {“ ...” [“ ...”。“ ...” +“ ... “-” ...“〜” ...“ not” ...“异步” ...“ await” ...“ False” ...“ True” ...“无”    ... ... ... ... ... ... ... ...“ \'” ...“ \”“ ...” \'\'\'“ ...” \“ \” \“” ...“ \'”    ...“ \”“ ...” \'\'\'“ ...” \“ \” \“” ...“ \'” ...“ \”“ ...” \'\ '\'“ ...” \“ \” \“” ...“ \'” ...“ \”“ ...” \'\'\'“ ...” \“ \” \“ “ ...

5 个答案:

答案 0 :(得分:0)

你是说吗?

elif sum(player_cards) > sum(computer_cards) and sum(player_cards) < 21:

and是布尔运算符。每边需要两个操作数。但是< 21不是表达式。

在Python中,您可以更简洁地编写为:

elif 21 > sum(player_cards) > sum(computer_cards):

答案 1 :(得分:0)

使用此:

elif (sum(player_cards) > sum(computer_cards)) and (sum(player_cards)< 21):

答案 2 :(得分:0)

您应该从代码中删除and

elif sum(player_cards) > sum(computer_cards) and < 21:

它应该像这样:

elif sum(player_cards) > sum(computer_cards) < 21

答案 3 :(得分:0)

该错误是无效的语法,请参见20> 10和<21,您将21与之进行比较?您也必须指定。

elif sum(player_cards)> sum(computer_cards)和<21应该是

elif sum(player_cards) > sum(computer_cards) and sum(computer_cards) < 21

 elif sum(player_cards) > sum(computer_cards) and sum(player_cards) < 21

根据您的要求

答案 4 :(得分:0)

好吧,如果我能清楚地了解您要做什么,那么我将可以为您提供帮助。 但是,这里的语法似乎不正确:

sum(player_cards)> sum(computer_cards)和<21相反,我会这样:

See image

注意使用方括号。变量需要在运算符的两侧进行测试。