|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
编写一个函数 findstr(),该函数统计一个长度为 2 的子字符串在另一个字符串中出现的次数。例如:假定输入的字符串为“You cannot improve your past, but you can improve your future. Once time is wasted, life is wasted.”,子字符串为“im”,函数执行后打印“子字母串在目标字符串中共出现 3 次”- def findstr(n,b):
- a = n.count(b)
- print(a)
- n = input("ready:")
- b = input("work:")
- print("%s in project counts is %s" % (b,a))
复制代码
- ready:1234156456461
- work:12
- 12 in project counts is <function a at 0x000001FD90282048>
复制代码
大神 我怎么就返回了一个函数? 如果 findstr("You cannot improve your past, but you can improve your future. Once time is wasted, life is wasted.") 则直接返回了一个整数型
print(a) 要改成 return a,因为后面打印了函数的返回值。另外,你都没有调用函数,a 从哪来的?
- def findstr(n, b):
- a = n.count(b)
- return a
- n = input("ready:")
- b = input("work:")
- print("%s in project counts is %s" % (b, findstr(n, b)))
复制代码
|
|