鱼C论坛

 找回密码
 立即注册
查看: 973|回复: 2

零基础第18课课后题答案运行不了

[复制链接]
发表于 2018-1-20 09:43:36 | 显示全部楼层 |阅读模式

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

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

x
用count来计数,没有任何显示。
而用小甲鱼的函数,运行的结构不论子字符串是否包含在内,运行结果都显示未找到。用的是win7,python3.41
用字符串count来寻找字符代码:
def findstr(str1,str2):
    if str2 not in str1:
        print('子字符串不在目标字符串中!')
    else:
        print('字符串在目标字符串出现:'+str1.count(str2)+'次')
        
str1=input('请输入目标字符串 :' )
str2=input('请输入子字符串 (两个字符):')
findstr=(str1,str2)

小甲鱼的答案代码
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)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-1-20 09:49:17 | 显示全部楼层
  1. def findStr(desStr, subStr):
  2.     count = 0
  3.     length = len(desStr)
  4.     if subStr not in desStr:
  5.         print('在目标字符串中未找到字符串!')
  6.     else:
  7.         for each1 in range(length-1):      
  8.             if desStr[each1] == subStr[0]:
  9.                 if desStr[each1+1] == subStr[1]:
  10.                     count += 1
  11.                     
  12.         print('子字符串在目标字符串中共出现 %d 次' % count)

  13. subStr = input('请输入目标字符串:')
  14. desStr = input('请输入子字符串(两个字符):')
  15. findStr(desStr, subStr)
复制代码

输入的那两个反了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-20 09:52:49 | 显示全部楼层
def findstr(str1,str2):
    if str2 not in str1:
        print('子字符串不在目标字符串中!')
    else:
        print('字符串在目标字符串出现:'+str(str1.count(str2))+'次')
        
str2=input('请输入目标字符串 :' )
str1=input('请输入子字符串 (两个字符):')
findstr(str1,str2)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-29 12:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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