1

To whom this may concern,

This is some code to a card game simulation. (The game of war). When I run it I receive this error:

player0.append(player1[range(warcard1 + 1)] 
      ^
SyntaxError: invalid syntax

I do not have a clue what the error is but maybe someone here does. Hopefully someone can resolve the issue for me. The code is, of course, still a work in progress so if there are any other bugs i would be happy to hear about them and their possible solutions.

import random

cards = ['ace', 'ace', 'ace', 'ace', '1', '1', '1', '1', '2', '2', '2', '2', '3', '3', '3', '3', '4', '4', '4', '4', '5', '5', '5', '5', '6', '6', '6', '6', '7', '7', '7', '7', '8', '8', '8', '8', '9', '9', '9', '9', '10', '10', '10', '10', 'jack', 'jack', 'jack', 'jack', 'queen', 'queen', 'queen', 'queen', 'king', 'king', 'king', 'king']

order = ['ace', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'jack', 'queen', 'king'] 

print "shuffling cards" 
random.shuffle(cards,random.random)
print "lets play"
player0 = [cards[i] for i in range(len(cards)) if i % 2 == 1]

player1 = [cards[i] for i in range(len(cards)) if i % 2 == 0]

while (len(player0) > 0 or len(player1) > 0):

    nextcard0 = player0[0]
    nextcard1 = player1[0]

    cardplayed0 = order.index(nextcard0)

    cardplayed1 = order.index(nextcard1)

    if cardplayed0 > cardplayed1:

        player0.append(nextcard0)
        player0.append(nextcard1)
        player0.remove(nextcard0)
        player1.remove(nextcard1)

    elif cardplayed0 < cardplayed1:

        player1.append(nextcard1)
        player1.append(nextcard0)
        player1.remove(nextcard1)
        player0.remove(nextcard0)

    elif cardplayed0 == cardplayed1:
        while warcardvalue0 == warcardvalue1:
            if len(player0) >= 4:
                warcard0 = 4
            elif len(player0) < 4:
                warcard0 = len(player0)

            if len(player1) >= 4:
                warcard1 = 4
            elif len(player1) < 4:
                warcard1 = len(player1)


            warcardvalue0 = order.index(warcard0)
            warcardvalue1 = order.index(warcard1)

            if warcardvalue0 > warcardvalue1:
                player0.append(player0[range(warcard0 + 1)]
                player0.append(player1[range(warcard1 + 1)] 
                player0.remove(player0[range(warcard0 + 1)]
                player1.remove(player1[range(warcard1 + 1)] 

            elif warcardvalue0 < warcardvalue1:
                player1.append(player1[range(warcard1 + 1)]
                player1.append(player0[range(warcard0 + 1)] 
                player1.remove(player1[range(warcard1 + 1)]
                player0.remove(player0[range(warcard0 + 1)] 
            else
                print "another war!" 


if len(player1) == 0:
    print "player1 won!"
elif len(player0) == 0:
    print "player0 won!"

2 Answers 2

5

The line indicated in the error message

player0.append(player1[range(warcard1 + 1)] 
                                           ^

is missing a closing )

In fact all of these lines are missing the closing paren:

       if warcardvalue0 > warcardvalue1:
            player0.append(player0[range(warcard0 + 1)]
            player0.append(player1[range(warcard1 + 1)] 
            player0.remove(player0[range(warcard0 + 1)]
            player1.remove(player1[range(warcard1 + 1)] 

        elif warcardvalue0 < warcardvalue1:
            player1.append(player1[range(warcard1 + 1)]
            player1.append(player0[range(warcard0 + 1)] 
            player1.remove(player1[range(warcard1 + 1)]
            player0.remove(player0[range(warcard0 + 1)] 

You may want to look through your code to make sure there are no other intstances of this problem.

Also, you might consider (and certainly would benefit) from an editor that does paren matching for you, and would alert you to similar problems. I use one, and it's definitely worth it.

If you are interested in such editors, this might be a place to start explore options: Top 10 Best Text Editors With Brace Matching

1
1

You are missing a closing parenthesis on every append/remove line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.