0

我不明白为什么我的elif陈述中有错误。

我正在将 eclipse 插件pydev与最新版本的 python 一起使用。

例如,

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

这应该输出为True

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

相反,它给了我这个:

“在第 42 行第 54 列遇到“<”。期待以下之一:“(”...“{”...“[”...“。”...“+”...“-” ...“〜”...“不”...“异步”...“等待”...“假”...“真”...“无”... ... .. . ... ... ... ... ... "\'" ... "\"" ... "\'\'\'" ... "\"\"\"" 。 .. "\'" ... "\"" ... "\'\'\'" ... "\"\"\"" ... "\'" ... "\"" 。 .. "\'\'\'" ... "\"\"\""... "\'" ... "\"" ... "\'\'\'" ... "\"\"\"" ...

4

5 回答 5

0

你的意思?

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

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

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

elif 21 > sum(player_cards) > sum(computer_cards):
于 2019-01-25T06:57:52.160 回答
0

用这个:

elif (sum(player_cards) > sum(computer_cards)) and (sum(player_cards)< 21):
于 2019-01-25T06:58:12.933 回答
0

您应该and从代码中删除:

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

它应该如下所示:

elif sum(player_cards) > sum(computer_cards) < 21
于 2019-01-25T06:58:57.183 回答
0

错误是无效语法,请参阅 20 > 10 和 < 21,您将 21 与什么进行比较?你也必须指定。

elif sum(player_cards) > sum(computer_cards) and < 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

根据您的要求

于 2019-01-25T06:59:03.180 回答
0

好吧,如果我能清楚地了解您要做什么,我将能够帮助编写代码。但是,这里的语法似乎不正确:

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

相反,我会这样做:

if (condition):
    <statements here>
elif ((sum(player_cards) > sum(computer_cards)) and (sum(player_cards)< 21)):
    <statements here>
else:
    return

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

于 2019-01-25T07:09:49.900 回答