简体   繁体   中英

How to call a function from another one with optional third parameter?

I'm learning python from this tutorial . I'm trying to solve 3rd exercise (under 7.16. Exercises, on this page ). If anybody can help me and give some tips because it's not that I don't know how, but I'm not sure I get what actually I have to do ("HOW TO DO" is not the problem, problem is "WHAT TO DO").

This is the code I have so far (from the 2nd exercise, since these two exercises are linked):

def count_letters(string, letter):
    count = 0
    for char in string:
        if char == letter:
            count+=1
    print count

An optional parameter is a parameter you can either provide or leave out, in the latter case, there's a default value assigned.

Given the second find in http://openbookproject.net/thinkcs/python/english2e/ch07.html#optional-parameters

def find(strng, ch, start=0):

start is an optional parameter. In case you in invoke find like

find('foobar', 'o')

start is implicitly set to 0, thus find starts finding at index 0, thus at the beginning of that string, in case you invoke it like

find('foobar', 'o', 3)

start = 3 would we used, and try start finding letter 'o' at index 3 (and fail).


Exercise 7.16.3 simply wants you to find starting at 0, get the index for the first match, use that index to figure out a new value to continue searching at, find again... and repeat and count matches until the find fails.

可选的第三个参数引用string.find方法的start参数,该方法允许您指定搜索的开始索引。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM