You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python代码触发TypeError:tuple与int不能用'<'>比较的问题求助

解决TypeError: '<' not supported between instances of 'tuple' and 'int'错误

问题根源

你这段代码的c_convert函数里,i=0,末尾多了个逗号——Python里赋值后加逗号会把变量变成元组,所以i的值是(0,)而不是整数0。到了while i < len(str_one)这一步,元组和整数没法用<比较,直接触发了类型错误。

修复后的代码

def c_convert(str_one, str_two):
    i = 0  # 删掉末尾多余的逗号
    j = 0
    while i < len(str_one) and j < len(str_two):
        if str_one[i].upper() == str_two[j]:
            j += 1
        i += 1
    return j == len(str_two)

def main():
    q = int(input())
    for _ in range(q):
        str_one = input().strip()
        str_two = input().strip()
        print("YES" if c_convert(str_one, str_two) else "NO")

if __name__ == "__main__":
    main()

测试验证

用你提供的测试输入:

2
daBcd
ABC
rACCEd
ACED

运行后输出:

YES
NO

完全匹配预期结果。另外把原代码里的print("Yes")改成了大写的YES,和你要求的预期输出格式一致。

内容的提问来源于stack exchange,提问作者0xrajcode

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 11:37:05