Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3输出的两个代码段之间的差异_Python_Python 3.x_Validation - Fatal编程技术网

Python3输出的两个代码段之间的差异

Python3输出的两个代码段之间的差异,python,python-3.x,validation,Python,Python 3.x,Validation,我在hackerrank上做了一些python3(基本python3)并解决了这个问题。我写了两个代码段,第一个提供了错误的答案,但第二个被接受。尽管如此,它们都提供了相同的输出。仅供参考,我的第一个代码片段甚至没有通过示例测试用例 代码片段一: if __name__ == '__main__': s = input() if str(s).isalnum(): print(True) else: success = False

我在hackerrank上做了一些python3(基本python3)并解决了这个问题。我写了两个代码段,第一个提供了错误的答案,但第二个被接受。尽管如此,它们都提供了相同的输出。仅供参考,我的第一个代码片段甚至没有通过示例测试用例

代码片段一:

if __name__ == '__main__':
    s = input()
    if str(s).isalnum():
        print(True)
    else:
        success = False
        for ch in s:
            if ch.isalnum():
                success = True
                print(True)
        if not success:
            print(False)

    if str(s).isalpha():
        print(True)
    else:
        success = False
        for ch in s:
            if ch.isalpha():
                success = True
                print(True)
        if not success:
            print(False)


    if str(s).isdigit():
        print(True)
    else:
        success = False
        for ch in s:
            if ch.isdigit():
                success = True
                print(True)
        if not success:
            print(False)

    if str(s).islower():
        print(True)
    else:
        success = False
        for ch in s:
            if ch.islower():
                success = True
                print(True)
        if not success:
            print(False)

    if str(s).isupper():
        print(True)
    else:
        success = False
        for ch in s:
            if ch.isupper():
                success = True
                print(True)
        if not success:
            print(False)
代码片段二:[被接受的一个]

if __name__ == '__main__':
    s = input()
    print (any(c.isalnum() for c in s))
    print (any(c.isalpha() for c in s))
    print (any(c.isdigit() for c in s))
    print (any(c.islower() for c in s))
    print (any(c.isupper() for c in s))
这两个代码片段的答案是相同的:五行新代码中有五行是真的。第一个片段出了什么问题? [我以前不知道蟒蛇有什么功能]
提前谢谢

这两种代码不相等。在第一个循环中,您已经为s中的每一个ch打印了一个布尔值-远远超过五行中的五个true。如果s是一个字符串,并且在所有五个测试中都为true,那么输出可能是相等的。请尝试另一个不…@SpghttCd,是的。对不起,我的错。那是逻辑上的错误!!!