Python基础4(函数和类)
函数
#函数 返回字典
def build_person(first_name,last_name):
person={
'first':first_name,'last':last_name}
return person
musician=build_person('s','zq')
print(musician)
{
'first': 's', 'last': 'zq'}
def build_person(first_name,last_name,age=''):
person={
'first':first_name,'last':last_name}
if age:
person['age']=age
return person
musician=build_person('s','zq',age=23)
print(musician)
{
'first': 's', 'last': 'zq', 'age': 23}
#用户输入姓名,输入q时退出
def get_name(first_name,last_name):
full_name=first_name + ' ' + last_name
return full_name.title()
while True:
print("\nplease tell your name:")
print("enter 'q' at any time to quit")
f_name=input("First name:")
if f_name=='q':
break
l_name=input("Last name:")
if l_name=='q':
break
name=get_name(f_name,l_name)
print("\nHello, "+name+"!")
please tell your name:
enter 'q' at any time to quit
First name:s
Last name:zq
Hello, S Zq!
please tell your name:
enter 'q' at any time to quit
First name:q
#使用函数修改列表
def print_models(unprinted_designs,completed_models):
"""模拟打印每个设计,直到没有未打印的为止
打印每个设计后,将其移到completed_models中
"""
while unprinted_designs:
current_design=unprinted_designs.pop()
print("Printing model:"+ current_design)
completed_models.append(current_design)
def show_completed_models(completed_models):
print("\nThe following models have been printed :")
for completed_model in completed_models:
print(completed_model)
unprinted_designs=['iphone','