正如标题所说,我试图在我的函数中添加str.count的返回。但是一直是0。建议需要。
def count_code(str):
for i in range(ord('a'), ord('z')+1):
new = str.count("co"+chr(i)+"e")
count = int(new)+ int(new)
print(count)
count_code('aaacodebbb')
你一直得到0的原因是你每次在循环中使用一个新的count
。
试试这个:
def count_code(str):
count = 0
for i in range(ord('a'), ord('z')+1):
new = str.count("co"+chr(i)+"e")
count += int(new)
print(count)
count_code('aaacodebbb')