海月清辉 发表于 2019-11-28 22:59:27

参数问题

编写一个函数 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.")则直接返回了一个整数型

冬雪雪冬 发表于 2019-11-29 09:59:06

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)))

zltzlt 发表于 2019-11-29 20:29:32

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]
查看完整版本: 参数问题