[Python]小甲鱼Python视频第018课(函数:灵活即强大)课后题及参考解答

# -*- coding: utf-8 -*-
"""
Created on Wed Mar  6 19:28:00 2019

@author: Administrator
"""
                                                  


"""


测试题:
      
0. 请问以下哪个是形参哪个是实参?
def MyFun(x):
    return x ** 3

y = 3
print(MyFun(y))


y是实参
x是形参



1. 函数文档和直接用“#”为函数写注释有什么不同?
    函数文档:用隐藏属性指向字符串,函数文档的内容会进内存
    #:文件注释,运行时内存里面是没有的

2. 使用关键字参数,可以有效避免什么问题的出现呢?
    确保传参时一一对应,指定对应防止传参出错
    
3. 使用help(print)查看print()这个BIF有哪些默认参数?分别起到什么作用?


Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='
', file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.
    
    
    
    sep 分隔符
    end 末尾
    file 输出对象
    flush 是否刷新缓存
    
4. 默认参数和关键字参数表面最大的区别是什么?
    ......风马流不相及。。。。。



动动手
0. 编写一个符合以下要求的函数:
    a) 计算打印所有参数的和乘以基数(base=3)的结果
    b) 如果参数中最后一个参数为(base=5),则设定基数为5,基数不参与求和计算。

1. 寻找水仙花数

题目要求:如果一个3位数等于其各位数字的立方和,则称这个数为水仙花数。例如153 = 1^3+5^3+3^3,因此153是一个水仙花数。编写一个程序,找出所有的水仙花数。


2. 编写一个函数 findstr(),该函数统计一个长度为 2 的子字符串在另一个字符串中出现的次数。例如:假定输入的字符串为“You cannot improve your past, but you can improve your future. Once time is wasted, life is wasted.”,子字符串为“im”,函数执行后打印“子字母串在目标字符串中共出现 3 次”。

3. 请写下这一节课你学习到的内容:格式不限,回忆并复述是加强记忆的好方式!
    额
"""




#动动手0
def BaseSum(*para,base = 3):

    result = 0 ;
    for each in para:
        result += each;
        

    result *= base;

    return result;



#动动手1:
def dds1_sxh():
    
    result = [];
    for each in range(100,1000):
        ge  = each % 10;
        shi = (each // 10) % 10;
        bai = (each // 100) % 10;
        if each == ge**3 + shi**3 + bai**3:
            result.append(each);
    
    print(result);
    

dds1_sxh();


#动动手2:
def dds2_findstr(str_full,str_sub):
    length_full = len(str_full);
    length_sub  = len(str_sub);
    result_count = 0;
    
    if length_sub>length_full or length_sub !=2:
        return None;
    
    for idx in range(length_full-1):
        if str_full[idx:idx+2] == str_sub:
            result_count += 1;
            
    return result_count;


str_full = "You cannot improve your past, but you can improve your future. Once time is wasted, life is wasted";
str_sub  = 'im';

print(dds2_findstr(str_full,str_sub))   
    

  

~不再更新,都不让我写公式,博客园太拉胯了
原文地址:https://www.cnblogs.com/alimy/p/10502815.html