函数章节练习_1
今天计划没完成,下周抽空完成函数章节的练习
print('-'*10,'Introducing Python_CN_Bill Lubanovic_Python语言及其应用','-'*10) print('-'*10,'定义并调用一个空函数','-'*10) def do_nothing(): pass #pass表名函数没有做任何事情,与这一页留空白同样的作用 do_nothing() #调用函数 print(do_nothing()) #显示调用函数的内容 print('-'*10,'定义一个无参数,但打印输出一个单词的函数','-'*10) def make_a_sound(): print('quack') make_a_sound() print('-'*10,'定义一个无参数,但返回值的函数','-'*10) def agree(): return True print(agree()) def agree_2(): return print(True) agree_2() if agree(): print('Splendid!') else: print('That was unexpected.') print('-'*10,'引入参数的函数','-'*10) def echo(anything): return anything + ' ' + anything echo('Rumplestiltskin') print(echo('Rumplestiltskin')) print('-'*10,'调用带参数的函数+If','-'*10) def commentary(color): if color == 'red': return "It's a tomato" elif color == "green": return "It's a green pepper" elif color == "bee purple": return "I don't know that it is,but only bees can see it" else: return "I've never heard of the color" + color + "." comment = commentary('blue') print(comment) print('-'*10,'有用的None','-'*10) thing = None if thing: print("It's some do_nothing") else: print("It's no thing") print('-'*10,'为了区分None和布尔值False','-'*10) if thing is None: print("It's nothing") else: print("It's something") print('-'*10,'4.7.1 位置参数','-'*10) def menu(wine, entree, dessert): return {'wine':wine, 'entree':entree, 'dessert':dessert} print(menu('chardonnay', 'chicker', 'cake')) print(menu('beef', 'bagel', 'bordeaux')) print('-'*10,'4.7.2 关键字参数','-'*10) print('关键字参数 = ', menu(entree='beef', dessert='bagel', wine='bordeaux')) print(menu('frontenac', dessert='flan', entree='fish')) print('-'*10,'4.7.3 指定默认参数','-'*10)
OnionYang@