Python 遍历字符串列表时出现的问题

Python 遍历字符串列表时出现的问题,python,Python,我的python代码编译不正确。问题是: 返回提供的集合中的集合数 Parameters: cards (list(str)) a list of twelve cards as 4-bit integers in base 3 as strings, such as ["1022", "1122", ..., "1020"]. Returns: (int) The number of sets in the h

我的python代码编译不正确。问题是: 返回提供的集合中的集合数

Parameters:
    cards (list(str)) a list of twelve cards as 4-bit integers in
    base 3 as strings, such as ["1022", "1122", ..., "1020"].
Returns:
    (int) The number of sets in the hand.
Raises:
    ValueError: if the list does not contain a valid Set hand, meaning
        - there are not exactly 12 cards,
        - the cards are not all unique,
        - one or more cards does not have exactly 4 digits, or
        - one or more cards has a character other than 0, 1, or 2.
这是我的密码:

def计数集(卡片):
num_set=len(卡片)
对于范围内的i(num_集):
对于范围内的j(数量集):
卡片测试=卡片[i]
对于范围内的m(len(卡片测试)):
如果设置了num_!=12:
raise VALUE ERROR(“不完全有12张卡,”)
伊里夫一世j:
如果卡片_测试==卡片[j]:
raise VALUE ERROR(“卡不都是唯一的,”)
elif len(卡片测试)!=4:
raise VALUE ERROR(“一张或多张卡没有精确的4位数字,或”)
elif卡测试[m]!=0或卡\u测试[m]!=1张或卡\u测试[m]!=问题就在这里
raise VALUE ERROR(“一张或多张卡的字符不是0、1或2。”)
其他:
返回num_集
打印(计数组([“1111”、“2222”、“1212”、“1222”、“1112”、“0011”、“0022”、“2211”、“1010”、“0000”、“1221”、“0220”))
这一行是错误的:

elif卡测试[m]!=0或卡\u测试[m]!=1张或卡\u测试[m]!=2:
应该是:

elif卡测试[m]!=0和卡\u测试[m]!=1和卡_测试[m]!=2:
这意味着如果
cards\u test[m]
不是0,也不是1,也不是2,则引发错误

写这篇文章的更好方法如下:

elif not cards\u test[m]在“012”中:

您的错误是什么?您采取了哪些步骤来调试代码?错误消息是什么?在哪一行?为什么有嵌套的
for
循环?它将返回此raise VALUE错误(“一张或多张卡的字符不是0、1或2”)。VALUE错误:一张或多张卡的字符不是0、1或2。“但是我不需要返回值错误问题在注释
#的行上——问题在这里
@AAAlex123@acme_2020您的条件
cards\u test[m]!=0或卡\u测试[m]!=1张或卡\u测试[m]!=2
错误,当选中的数字与0、1或2不同时引发异常,因此每次都会引发异常