参数问题
编写一个函数 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.")则直接返回了一个整数型 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))) 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)))
页:
[1]