为什么这个 elif 语句会给我一个错误?
why does this elif statement give me an error?
我不明白为什么我的 elif
语句中出现错误。
我正在使用带有最新版本 python 的 eclipse 插件 pydev
。
例如,
if sum(player_cards) = 14 and sum(computer_cards) = 10
这应该输出为 True
elif sum(player_cards) > sum(computer_cards) and < 21 :
相反,它给了我这个:
"Encountered "<" at line 42, column 54. Was expecting one of: "(" ... "{" ... "[" ... "." ... "+" ... "-" ... "~" ... "not" ... "async" ... "await" ... "False" ... "True" ... "None"
... ... ... ... ... ... ... ... "\'" ... "\"" ... "\'\'\'" ... "\"\"\"" ... "\'"
... "\"" ... "\'\'\'" ... "\"\"\"" ... "\'" ... "\"" ... "\'\'\'" ... "\"\"\"" ... "\'" ... "\"" ... "\'\'\'" ... "\"\"\"" ...
你的意思是?
elif sum(player_cards) > sum(computer_cards) and sum(player_cards) < 21:
and
是布尔运算符。它的每一侧都需要两个操作数。但是 < 21
不是表达式。
在Python中,可以更简洁地写成:
elif 21 > sum(player_cards) > sum(computer_cards):
使用这个:
elif (sum(player_cards) > sum(computer_cards)) and (sum(player_cards)< 21):
您应该从代码中删除 and
:
elif sum(player_cards) > sum(computer_cards) and < 21:
它应该是这样的:
elif sum(player_cards) > sum(computer_cards) < 21
错误是语法无效,请参阅 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
取决于您的要求
好吧,如果我能清楚地了解您正在尝试做什么,我将能够帮助您编写代码。
但是,这里的语法似乎不正确:
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
注意括号的使用。需要在 和 运算符的任一侧测试变量。
我不明白为什么我的 elif
语句中出现错误。
我正在使用带有最新版本 python 的 eclipse 插件 pydev
。
例如,
if sum(player_cards) = 14 and sum(computer_cards) = 10
这应该输出为 True
elif sum(player_cards) > sum(computer_cards) and < 21 :
相反,它给了我这个:
"Encountered "<" at line 42, column 54. Was expecting one of: "(" ... "{" ... "[" ... "." ... "+" ... "-" ... "~" ... "not" ... "async" ... "await" ... "False" ... "True" ... "None" ... ... ... ... ... ... ... ... "\'" ... "\"" ... "\'\'\'" ... "\"\"\"" ... "\'" ... "\"" ... "\'\'\'" ... "\"\"\"" ... "\'" ... "\"" ... "\'\'\'" ... "\"\"\"" ... "\'" ... "\"" ... "\'\'\'" ... "\"\"\"" ...
你的意思是?
elif sum(player_cards) > sum(computer_cards) and sum(player_cards) < 21:
and
是布尔运算符。它的每一侧都需要两个操作数。但是 < 21
不是表达式。
在Python中,可以更简洁地写成:
elif 21 > sum(player_cards) > sum(computer_cards):
使用这个:
elif (sum(player_cards) > sum(computer_cards)) and (sum(player_cards)< 21):
您应该从代码中删除 and
:
elif sum(player_cards) > sum(computer_cards) and < 21:
它应该是这样的:
elif sum(player_cards) > sum(computer_cards) < 21
错误是语法无效,请参阅 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
取决于您的要求
好吧,如果我能清楚地了解您正在尝试做什么,我将能够帮助您编写代码。 但是,这里的语法似乎不正确:
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
注意括号的使用。需要在 和 运算符的任一侧测试变量。