鱼C论坛

 找回密码
 立即注册
查看: 1479|回复: 3

[已解决]18讲 中count是怎么计算的?

[复制链接]
发表于 2017-3-30 21:16:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
def findStr(desStr, subStr):
    count = 0
    length = len(desStr)
    if subStr not in desStr:
        print('在目标字符串中未找到字符串!')
    else:
        for each1 in range(length-1):      
            if desStr[each1] == subStr[0]:
                if desStr[each1+1] == subStr[1]:
                    count += 1
                    
        print('子字符串在目标字符串中共出现 %d 次' % count)

desStr = input('请输入目标字符串:')
subStr = input('请输入子字符串(两个字符):')
findStr(desStr, subStr)


这里的count是怎么计算的?然后%d 为什么用在这里了?后面为什么要加上 count?         求详细解释
最佳答案
2017-3-30 21:26:09
本帖最后由 新手·ing 于 2017-3-30 21:27 编辑

count 最初是0
for each1 in range(length-1):      
            if desStr[each1] == subStr[0]:
                if desStr[each1+1] == subStr[1]:
如果这一段条件为真,count加1
  1. print(print(''子字符串在目标字符串中共出现' + count + '次'')
复制代码


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-3-30 21:26:09 | 显示全部楼层    本楼为最佳答案   
本帖最后由 新手·ing 于 2017-3-30 21:27 编辑

count 最初是0
for each1 in range(length-1):      
            if desStr[each1] == subStr[0]:
                if desStr[each1+1] == subStr[1]:
如果这一段条件为真,count加1
  1. print(print(''子字符串在目标字符串中共出现' + count + '次'')
复制代码


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-3-30 21:42:56 | 显示全部楼层
新手·ing 发表于 2017-3-30 21:26
count 最初是0
for each1 in range(length-1):      
            if desStr[each1] == subStr[0]:

好的,谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-30 21:55:07 | 显示全部楼层

求最佳...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-10 09:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表