首页 > 解决方案 > 当我运行此代码时,(打印)功能不起作用

问题描述

当我运行此代码时,该print功能不起作用,所以我试图移动它并检查是否有任何错误,但它根本不起作用。

def is_substring(small,big):
    count=0

    for move in range(len(big)):

        if big[move:move+len(small)] == small:
            return True
            count+=1

    return False


    print(f"we found {count} similar words")


is_substring('hi','hi and Hello or hi')

标签: pythonpython-3.x

解决方案


看起来你return在 print 被调用之前 -ing 。

return 'something'

将退出函数,传'something'回函数调用的来源,因此return True之后跳过任何内容。

尝试在所有循环之后检查 count 的值:

def is_substring(small,big):
    count=0    
    for move in range(len(big)):
        if big[move:move+len(small)] == small:
            count+=1

    print(f"we found {count} similar words")
    return count != 0

is_substring('hi','hi and Hello or hi')

推荐阅读