0

Hi you might remember this program if you are a regular here. I have solved many of the bugs but am stumped by one. The error is:

File "/Users/administrator/Desktop/war.py", line 62, in <module>
    player1.extend(player1[range(warcardvalue1)])
TypeError: list indices must be integers, not list

the code is:

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'] 
warcardvalue0 = 0
warcardvalue1 = 0
print "shuffling cards" 
random.shuffle(cards)
print "lets play"

player0 = cards[1::2]

player1 = cards[::2]

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) >= 3:
                        warcard0 = player0[3]
                elif len(player0) < 3:
                    warcard0 = player0[len(player0)-1]

            if len(player1) >= 3:
                        warcard1 = player1[3]
                elif len(player1) < 3:
                    warcard1 = player1[len(player1)-1]


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

            if warcardvalue0 > warcardvalue1:
                player0.extend(player0[range(warcardvalue0)])
                player0.extend(player1[range(warcardvalue1)])
                player0.extend(player0[range(warcardvalue0)])
                player1.extend(player1[range(warcardvalue1)]) 

            elif warcardvalue0 < warcardvalue1:
                player1.extend(player1[range(warcardvalue1)])
                player1.extend(player0[range(warcardvalue0)])
                player1.extend(player1[range(warcardvalue1)])
                player0.extend(player0[range(warcardvalue0)]) 
            else:
                print "another war!" 


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

I think the problem is that you cant use range in lists but I'm not sure.

3
  • 1
    The error is just what it says. range returns a list. You can't use a list as an index into another list. Explain what you're trying to achieve with player1[range(warcardvalue1)].
    – BrenBarn
    Aug 23, 2012 at 17:04
  • You probably want slice notation. Aug 23, 2012 at 17:10
  • May I recommend code.activestate.com/recipes/sets/13 if you are interested in a Python version of War Game? Aug 23, 2012 at 20:25

1 Answer 1

1

I think the problem is that you cant use range in lists but I'm not sure.

The problem is that you can't index a list with a list.

Try this instead:

player1.extend(player1[:warcardvalue1])

Seems a bit odd to want to do it though....

2
  • now im receiving this error: File "/Users/administrator/Desktop/war.py", line 58, in <module> player0.remove(player0[x] for x in range(warcardvalue0)) ValueError: list.remove(x): x not in list
    – A. Collins
    Aug 23, 2012 at 17:13
  • @A.Collins: That's a different line. It's not related to your original error. This should help you: java2s.com/Code/Python/List/Listdeleteaslice2.htm
    – Mark Byers
    Aug 23, 2012 at 17:15

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.