-2

I'm new to python so if my card looks very beginner-ish that is why.

I'm trying to compare two cards and find which one is bigger. In this scenario, red beats black, yellow beats red and black beats yellow (like a digital rock paper scissors) I also intend on comparing the numbers of the cards if the suits / colours are the same

This is what I've got so far:

import random


vals = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
colours = ['red', 'yellow', 'black']

deck = list(itertools.product(vals, colours))

random.shuffle(deck)

for vals, colours in deck:
    print( str(vals) + ' ' + str(colours))




p1card = str(deck[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28])
p2card = str(deck[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29])
  

print(player_one + "'s card = " + str(p1card))
print(player_two + "'s card = " + str(p2card))

if p1card.find('red') and p2card.find('black'):
    print(player_one + ' wins!')

if p1card.find('yellow') and p2card.find('red'):
    print(player_one + ' wins!')

if p1card.find('black') and p2card.find('yellow'):
    print(player_one + ' wins!')

if p2card.find('red') and p1card.find('black'):
    print(player_two + ' wins!')

if p2card.find('yellow') and p1card.find('red'):
    print(player_two + ' wins!')

if p2card.find('black') and p1card.find('yellow'):
    print(player_two + ' wins!')

When I run the code I get the error:

TypeError: list indices must be integers or slices, not tuple

I dont know what this means or how I'm going to even do this task so any help would be very appreciated!

Thanks!

  • 2
    "_I dont know what this means_" Did you do any searching first? I just googled the error message and found tons of posts on this site about it. Here's a couple - https://stackoverflow.com/questions/21662532/python-list-indices-must-be-integers-not-tuple/21662608, https://stackoverflow.com/questions/46275675/typeerror-list-indices-must-be-integers-or-slices-not-tuple-for-list-of-tuples. – takendarkk Oct 19 '21 at 19:39
  • What do you expect `deck[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28]` to do, exactly? This is what's causing the issue. – Green Cloak Guy Oct 19 '21 at 19:39

1 Answers1

0

That's not how lists work in python. What you probably expect is

deck = [0,10,20,30,40,50]
deck[0,2,4] # you want this [0, 20, 40] but an error is thrown
deck[1,3,5] # you want this [10, 30, 50] but an error is thrown

Sadly, it doesn't work that way. You can only get one number from it, like so:

deck = [0,10,20,30,40,50]
deck[1] # returns 10
deck[4] # returns 40
deck[1,4] # error

You can, however, do this:

deck = [0,10,20,30,40,50]
deck[::2] # returns [0, 20, 40]
deck[1::2] # returns [10, 30, 50]

What I'm doing here is called slicing

Samuel
  • 378
  • 1
  • 9