Search code examples
pythonand-operator

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" ... ... ... ... ... ... ... ... "\'" ... "\"" ... "\'\'\'" ... "\"\"\"" ... "\'" ... "\"" ... "\'\'\'" ... "\"\"\"" ... "\'" ... "\"" ... "\'\'\'" ... "\"\"\"" ... "\'" ... "\"" ... "\'\'\'" ... "\"\"\"" ...


Solution

  • 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.