1. 程式人生 > >python第九天:簡單瞭解下函式

python第九天:簡單瞭解下函式

簡單瞭解了下函式,近期將通訊錄系統中的重複程式碼用函式封裝起來。

'''列印列表的函式'''
def print_names (unprinted_names,completed_names):
    while unprinted_names:
        name = unprinted_names.pop()
        completed_names.append(name)
def show (completed_names):
    for names in completed_names:
        print("The person:"+names.title()+" has been checked!")
 
unprinted_names = ["zhang xu","wu you","gu er cheng"]
completed_names = []
print_names(unprinted_names[:],completed_names) '''此處是操作的列表切片,保持原列表內容不
                                                   變'''
show (completed_names)
print (unprinted_names)

'''多元素作為實參時的元祖處理'''
def subject_choose (name,*subject):
    print ("The student: "+name.title()+" choose the subject following:")
    for subjects in subject:
        print ("- "+subjects.upper())
subject_choose('zhang xu',"chinese",'math','english','computer science')
subject_choose("wu you","chinese",'math')

'''多元素作為實參的字典處理'''
def build_profile(**usr_info):
    information = {}
    for key,value in usr_info.items():
        information[key]=value
    return information
user_info = build_profile(name='zhang xu',home='da qiao',telephon_number='1234')
print(user_info)
'''外部函式的匯入,pp函式即為例一的列印函式'''
import pp as sb
 
sb.subject_choose('wu you','chinese','math','english')
sb.subject_choose("zhang xu",'chinese','pe','music','computer')