-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrans_list.py
More file actions
executable file
·94 lines (72 loc) · 2.81 KB
/
Copy pathtrans_list.py
File metadata and controls
executable file
·94 lines (72 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
def greet_users(names):
"""向列表中每位用户发出简单的问候"""
for name in names:
msg = "Hello, " + name.title() + "!"
print(msg)
usernames = ['hannah', 'coohx', 'margot']
# 将用户列表传递给函数
greet_users(usernames)
# 模拟3D打印工作
def print_models(unprinted_designs, completed_models):
"""
模拟打印每个设计,直到没有未打印的设计为止
打印每个设计后,都将其移到列表completed_models中
"""
while unprinted_designs:
current_design = unprinted_designs.pop()
# 模拟3D模型打印过程
print("Printing model: " + current_design)
completed_models.append(current_design)
def show_completed_models(completed_modles):
"""显示打印好的所有模型"""
print("\nThe following models have been printed:")
for complete_model in completed_models:
print(complete_model)
# 创建未打印的设计列表
unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []
# 调用打印函数和显示函数
# 列表传递给函数,函数就可对其修改,这个修改是永久性的
#print_models(unprinted_designs, completed_models)
show_completed_models(completed_models)
# 列表已经被函数修改,输出为空列表
# print(unprinted_designs)
# 传递列表副本,不影响原始列表(切片实现)
print_models(unprinted_designs[:], completed_models)
show_completed_models(completed_models)
# 列表已经被函数修改,输出为空列表
print(unprinted_designs)
# 用户简介profile
# **user_info中的两个星号让Python创建一个user_info的空字典,并将接收到的
#所有名称-值都封装到这个字典中
def build_profile(first, last, **user_info):
"""创建一个字典,其中包含我们知道的有关用户的一切"""
profile = {}
# 将用户的姓&名加入到字典中
profile['first_name'] = first
profile['last_name'] = last
# 遍历字典user_info中的键-值对
for key, value in user_info.items():
profile[key] = value
# 返回字典
return profile
# 向函数传递必选参数和任意数量的key-value参数
user_profile = build_profile('xin', 'huang',
location = 'XAUT',
field = 'Linux')
print(user_profile)
# {'last_name': 'huang', 'location': 'XAUT', 'field': 'Linux', 'first_name': 'xin'}
# 我的信息
def my_info(first, last, **user_info):
my_profile = build_profile(first, last, **user_info)
print(my_profile)
for info in my_profile:
"""info 会循环字典的key"""
print(info + ': ' + str(my_profile[info]))
# 统计我的信息,并打印
my_info('xin', 'huang',
age = 21,
gender = 'M',
field = 'Linux')