【发布时间】:2021-12-06 17:29:06
【问题描述】:
我是 python 新手,所以如果我的卡看起来很初学者,那就是原因。
我正在尝试比较两张卡片并找出哪一张更大。在这种情况下,红色战胜黑色,黄色战胜红色,黑色战胜黄色(就像数字石头剪刀布一样)如果花色/颜色相同,我还打算比较卡片的数量
这是我目前得到的:
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!')
当我运行代码时出现错误:
TypeError: list indices must be integers or slices, not tuple
我不知道这意味着什么,也不知道我将如何完成这项任务,因此非常感谢任何帮助!
谢谢!
【问题讨论】:
-
"我不知道这是什么意思" 你先搜索了吗?我刚刚用谷歌搜索了错误消息,并在这个网站上发现了大量关于它的帖子。这是一对 - stackoverflow.com/questions/21662532/…,stackoverflow.com/questions/46275675/…。
-
你希望
deck[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28]做什么,确切地说?这就是导致问题的原因。
标签: python