function exercise

''' Write a function to count numbers, letters, spaces, and others in the incoming string ''' 
def func(n):
     # s = input("Please input >>>:") 
    num = 0
    zimu = 0
    kongge = 0
    default = 0
    for i in n:
        if i.isdigit():
            num += 1
        if i.isalpha():
            zimu += 1
        if i.isspace():
            kongge += 1
         else :
            default += 1
    return num,zimu,kongge,default
    # print(num)
print(func('nihao ,123 '))

''' Write a function to determine whether the length of the object (string, list, tuple) passed in by the user is greater than 5 ''' 
def length(s):
     if len(s) > 5 :
         print (len(s))
         return True
     else :
         print (len(s))
         return False
 print (length( ' 123456 ' ))
 print (length([1,2,3,4,5,6,7 ]))
 print (length((1, ' as ' ,[1,2 ])))
 print (length({1:2, ' as ' :1, ' kn ' :[1,2]}))

''' Write a function similar to len ''' 
def length(s):
    length = 0
    for i in s :
        length += 1
    return length
print(length('123456'))

'''
Write a function that checks the length of the incoming list, if it is greater than 2,
then just keep the first two lengths and return the new content to the caller.
''' 
def func(s):
     if len(s) >2 :
         return s[:2 ]
     else :
         return  " Cannot calculate Chinese " 
print (func([1,2,3,4,5,6 ]))
 print (func( ' hello ' ))
 print (func( ' abcd ' ))

'''
Write a function that checks to get the elements corresponding to all odd bit indices of the incoming list or tuple object,
and return it to the caller as a new list.
'''

def func(s):
    list = s[1::2]
    return list
print(func([1,2,3,4,5,6,7,8,9]))

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325352591&siteId=291194637
Recommended