1 # time 2021/11/11
2 """8.1函数的定义"""
3 """
4 语法 : def 函数名():
5 函数体
6 函数名()
7 8.1.1 向函数传递信息
8 语法 : def 函数名(args):
9 函数体
10 函数名(abc)
11 8.1.2 形参与实参:
12 形参: 定义时 写写入函数名括号里面的参数
13 实参: 调用时写入的参数
14 实参 传给 形参 代入到函数体中
15 """
16 """8.2传递实参"""
17 """
18 8.2.1位置参数
19 def describe_pet(animal_type, pet_name)
20 print(f'{animal_type}, {pet}')
21
22
23 describe_pet('pig', 'haiyuan' )
24 8.2.2关键字参数
25 def describe_pet(animal_type, pet_name)
26 print(f'{animal_type}, {pet}')
27
28 describe_pet(animal_type = 'pig', pet_name = 'haiyuan' )
29 8.2.3默认值
30 def describe_pet(animal_type, pet_name='yuxin')
31 print(f'{animal_type}, {pet}')
32
33 describe_pet(animal_type = 'pig' )
34 note : 使用默认值时,在形参列表中必须先列出没有默认值的形参,再列出有默认值的实参,这让python依然可以准确的解读位置参数
35 note : 默认值参数也是可以修改的
36 """
37 """8.3返回值"""
38 """
39 8.3.1 返回简单值
40 def get_formatted_name(first_name, last_name):
41 full_name = first_name + last_name
42 return full_name.title()
43
44 musician = get_formatted_name('jimi','hendrix')
45 print(musician)
46
47 8.3.2
48
49 """
50
51
52 def get_formatted_name(first_name, last_name, middle_name=''):
53 if middle_name:
54 print(f'{first_name.title()}, {last_name.title()}, {middle_name.title()}')
55 else:
56 print(f'{first_name.title()}, {last_name.title()}')
57
58
59 get_formatted_name('yuxin1', 'abc12')
60 get_formatted_name('yuxin2', 'abc32', 'niehaiyuan')
61
62
63 # 8-1
64 def display_message():
65 print('本章学的内容是函数')
66
67
68 display_message()
69
70
71 # 8-2
72 def favorite_book(title):
73 print(f'my favorite book is {title}')
74
75
76 favorite_book('python进阶')
77
78
79 # 8-3
80 def make_shirt(size, fort):
81 print(f'T恤的尺码是{size},字样是{fort}')
82
83
84 make_shirt(175, '中国红')
85
86
87 # 8-4
88 def make_shirt(size, fort='I love Python'):
89 print(f'T恤的尺码是{size},字样是{fort}')
90
91
92 make_shirt('大号')
93 make_shirt('中号')
94 make_shirt('小号', 'PYHTON')
95
96
97 # 8-5
98 def describe_city(name, country):
99 print(f'{name.title()} is in {country.title()}')
100
101
102 describe_city('beijing', 'China')
103 describe_city('reykjavik', 'iceland')
104
105
106 # 8-6
107 def city_country(name, country):
108 s = f'"{name}, {country}"'
109 return s
110
111
112 print(city_country('beijing', 'China'))
113 print(city_country('东京', '日本'))
114 print(city_country('洛杉矶', '美国'))
115
116 # 8-7
117
118
119 # def make_album(singer_name, paper_name, num=''):
120 # dic = {}
121 # dic[singer_name] = paper_name
122 #
123 # if not num:
124 # return dic
125 # else:
126 # dic['专辑数量'] = num
127 # return dic
128 #
129 #
130 # print(make_album('yuxin1', 'abc123', num=10))
131 # print(make_album('yuxin2', 'def456'))
132
133 # 8-8
134 dic = {}
135 while 1:
136 singer_name = input("请输入歌手的名字:")
137 if singer_name == "0":
138 break
139 paper_name = input("请输入专辑的专辑:")
140 if paper_name == "0":
141 break
142 dic[singer_name] = paper_name
143 print('输入0为退出')
144
145
146 def make_album():
147 if len(dic) == 0:
148 return '无信息'
149 return dic
150
151
152 print(make_album())
153 # 2021/11/11