简体   繁体   中英

why does this elif statement give me an error?

I don't understand why I'm getting an error in my elif statement.

I'm using the eclipse plugin pydev with the latest version of python.

For example,

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

this should output as True

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

instead it gives me this:

"Encountered "<" at line 42, column 54. Was expecting one of: "(" ... "{" ... "[" ... "." ... "+" ... "-" ... "~" ... "not" ... "async" ... "await" ... "False" ... "True" ... "None" ... ... ... ... ... ... ... ... "\\'" ... "\\"" ... "\\'\\'\\'" ... "\\"\\"\\"" ... "\\'" ... "\\"" ... "\\'\\'\\'" ... "\\"\\"\\"" ... "\\'" ... "\\"" ... "\\'\\'\\'" ... "\\"\\"\\"" ... "\\'" ... "\\"" ... "\\'\\'\\'" ... "\\"\\"\\"" ...

Did you mean?

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

The and is a boolean operator. It needs two operands on each side. But < 21 is not an expression.

In Python, you can write it more concisely as:

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

用这个:

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

You should remove and from your code:

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

It should look like this:

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

the error is invalid syntax, see 20 > 10 and < 21, what are you comparing 21 with? you have to specify that too.

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

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

or

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

depending on your requirement

Well, If I could get a clear concept of what you are trying to do I would be able to help with the code. But, Here it seems the syntax is not right:

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

instead I would do it like this:

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

Notice the use of brackets. Variables need to be tested on either sides of the and operator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM