如何修复“TypeError:使用时不支持|:”“list”“和”“list”“的操作数类型”重新匹配

2024-04-23 18:24:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图检查用户输入是否与卡的正则表达式匹配 理论上[(2-9)|(10)|(AKQJ)][HCSD] 实际上“[(2-9)|(10)|(AKQJ)][HCSD]”

删除并重新键入所有WhitePase 开始全匹配线(不再出错) 每一个国家 移除正则表达式的非相关部分 从fullmatch拆分if语句 检查regex是如何工作的,但没有注意到这是一个刺痛,它需要引用

#!/usr/bin/python3
from re import fullmatch
def cardCheck(card):
    x = 1
    #if fullmatch([2-9|(10)|AKQJ][HCSD],card) == None:
    if x == 1:
        match = fullmatch((([2-9]|[(10)])|[AKQJ]),card)
        #return "False"
        return False
    else:
        return True
while True:
    card = input("enter the name of the card")
    if card == exit:
        break()
    card = card.upper()
    if cardCheck(card):
        print (card, "is a valid card")

我希望KC、9D、10S的卡“是有效卡”,希望11H、0s、23H没有输出 实际输出为

return False
         ^
SyntaxError: invalid syntax

或者

    match = fullmatch((([2-9]|[(10)])|[AKQJ][HCSD]),card)
TypeError: unsupported operand type(s) for |: 'list' and 'list'
depending on what version i tried

Tags: thefalsetrue键入returnif检查用户match