python - 当我运行此代码时,(打印)功能不起作用
问题描述
当我运行此代码时,该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')
解决方案
看起来你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')
推荐阅读
- css - 如何使用 Vue b-tab CSS 样式覆盖 Bootstrap?
- javascript - Vuetify 轮播图像未缩放到视口
- git - 具有截断历史的第二个远程回购策略
- visual-foxpro - 使用“独占使用”的 Visual Foxpro“正在使用的文件”
- php - 我想找到所有 A Href 并获得他们的链接和他们的全部内容
- xaml - 类型文本框未找到
- sql-server-2008 - 为什么我要检查 SQL Server 中的日期是否小于 0 或小于 1900 年
- java - 为什么我能够使用完全不同的类型创建一种类型的泛型集合?
- python - AWS Lambda 和 MySQL 连接处理
- python - WTForms:将另一个容器传递给验证器