Small turtle Lesson 16 Homework reflection summary

0. We list based on common characteristics, Ganso and strings, put them three collectively Why?

Collectively referred to as a sequence, they have three common features:
1. The index can be obtained by each element of
2. The default value is always the index starts from 0 (of course also supports flexible Python negative indexes)
3. The method of fragmentation by to give a range of elements set
4. there are many common operator (operator repeated, splicing operators, relational operators members)

4. Alas, now the kid actor too naughty, naughty neighbor's child, the little turtle just written the code drew a pattern, fish oil recovery at the trouble you ah, another picture of this guy is god horse? How so familiar ah! ?

Here Insert Picture Description
The picture Reprinted from: https: //www.jianshu.com/p/030b98ee6a5c

if name==each[0]:

if name!=each[0]

#注意;序列中的序列提取元素时,当运用 for in ,则直接对对象each进行提取,因为每次只会提取序列中的一个元素,因此进入了第一层

** programming problem

  1. Video we said sum () The BIF has a flaw, that is, if there is a string type parameter in the words will error, please write a new implementation process, automatically "ignore" parameter in the string and return the correct results . **
def sum(x):
    both = 0
    for each in x:
        if each.isdigit():
            both += each
        elif not each.isdigit():
            continue
    return both

number = '123456789a'
print(sum(list(number)))
#会报错:TypeError: unsupported operand type(s) for +=: 'int' and 'str'
这里若有大神知道具体报错原因请评论一下


改进:def sum(x):
    both = 0
    for each in x:
        if each.isdigit():
            both += int(each)
        elif not each.isdigit():
            continue
    return both

number = '12v3v4v567c89a'
print(sum(number))

Published 17 original articles · won praise 1 · views 357

Guess you like

Origin blog.csdn.net/cccccccaaaaaaaaa/article/details/105292228