第8章 函數 4-8節

# 8.4.1 在函數中修改列表
def greet_user(names):
  '''向列表中的每位用戶都發出簡單的問候'''
  for name in names:
    msg = "Hello, " + name.title() + "!"
    print(msg)

usernames = ['star','james','wendy']
greet_user(usernames)python

# 不使用函數修改列表
# 首先建立一個列表,其中要包含一些打印的設計
unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []app

# 模擬打印每一個設計,直到沒有未打印的設計爲止
# 打印每一個設計後,都將其移到列表completed_models中
while unprinted_designs:
  current_design = unprinted_designs.pop()

  # 模擬根據設計製做3D打印模型的過程
  print("Printing model: " + current_design)
  completed_models.append(current_design)

# 顯示打印好的全部模型
print("\nThe following models have been printed:")
for completed_model in completed_models:
  print(completed_model)

# 使用函數修改列表
def print_models(unprinted_designs, completed_models):
  '''
  模擬打印每一個設計,直到沒有未打印的設計爲止
  打印每一個設計後,都將其移到列表completed中
  '''
  while unprinted_designs:
    current_design = unprinted_designs.pop()

# 模擬根據設計製做3D打印模型的過程
print("Printing model: " + current_design)
completed_models.append(current_design)

def show_completed_models(completed_models):
  '''顯示打印好的全部模型'''
  print("\nThe following models hvae been printed:")
  for completed_model in completed_models:
    print(completed_model)

unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []
print_models(unprinted_designs, completed_models)
show_completed_models(completed_models)ssh

# 8.4.2 禁止函數修改列表——function_name(lsit_name[:]),切片表示法[:]建立列表的副本
def print_models(unprinted_designs, completed_models):
  '''
  模擬打印每一個設計,直到沒有未打印的設計爲止
  打印每一個設計後,都將其移到列表completed中
  '''
  while unprinted_designs:
    current_design = unprinted_designs.pop()

# 模擬根據設計製做3D打印模型的過程
print("Printing model: " + current_design)
completed_models.append(current_design)

def show_completed_models(completed_models):
  '''顯示打印好的全部模型'''
  print("\nThe following models hvae been printed:")
  for completed_model in completed_models:
    print(completed_model)

unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []
print_models(unprinted_designs[:], completed_models)
show_completed_models(completed_models)iphone

 

# 8-9 魔術師
def show_magicians(names):
  '''呈現列表中的名字'''
  for name in names:
  msg = "Welcome international magician, " + name.title() + "!"
  print(msg)

usernames = ['james','wendy','star']
show_magicians(usernames)

# 8-10 了不得的魔術師
def show_magicians(list_name, modify_infor):
  '''
  修改列表中的每一個信息,直到修改完成
  修改後的信息,都將其移到modify_infor中
  '''
while list_name:
  current_names = list_name.pop()
# 修改信息的過程
  print("\nModify information: " + current_names)
  modify_infor.append(current_names)

def make_great(modify_info):
  '''顯示修改好的信息'''
  print("\nThe following information have benn modified:")
  for modify_info in modify_infor:
    print("The great magician is " + modify_info)

list_name = ['james','star','wendy']
modify_infor = []函數

show_magicians(list_name,modify_infor)
make_great(modify_infor)
print(list_name) # 原列表信息已經完全變空ui

# 8-11 不變的魔術師
def show_magicians(list_name, modify_infor):
  '''
  修改列表中的每一個信息,直到修改完成
  修改後的信息,都將其移到modify_infor中
  '''
  while list_name:
    current_names = list_name.pop()

# 修改信息的過程
  print("\nModify information: " + current_names)
  modify_infor.append(current_names)

def make_great(modify_info):
  '''顯示修改好的信息'''
  print("\nThe following information have benn modified:")
  for modify_info in modify_infor:
    print("The great magician is " + modify_info)

list_name = ['james','star','wendy']
modify_infor = []spa

show_magicians(list_name[:],modify_infor)
make_great(modify_infor)
print(list_name) # 原列表信息沒有改變設計

 

# 任意數量實參
def make_pizza(*toppings):
  '''打印顧客點的全部配料'''
  print(toppings)

make_pizza('pepperoni')
make_pizza('msshrooms','green peppers','extra cheese')orm

# 8.5.1 結合使用位置實參和任意數量實參
def make_pizza(size, *toppings):
'''概述要製做的披薩'''
  print("\nMaking a " + str(size) +
  "-inch pizza with the following toppings:")
  for topping in toppings:
    print("- " + topping)ip

make_pizza(16,'pepperoni')
make_pizza(12,'msshrooms','green peppers','extra cheese')

# 8.5.2 使用任意數量的關鍵字實參
def build_profile(first, last, **user_info): # 老是接受名和姓,還接受任意數量的關鍵字實參
  '''建立一個字典,其中包含咱們知道的有關於用戶的一切信息'''
  profile = {}
  profile['first_name'] = first # 匹配鍵-值對
  profile['last_name'] = last # 匹配鍵-值對
  for key, value in user_info.items():
    profile[key]=value # 定義profile鍵_值對
  return profile

user_profile = build_profile('albert','einstin',
             location='princeton',
             filed='pyhsics')
print(user_profile)


# 8-12 三明治
def make_sandwich(*toppings):
  '''打印客戶要點的材料'''
  print(toppings)

make_sandwich('tomato')
make_sandwich('cheese', 'meat', 'boun')

# 8-13 用戶簡介:
def build_profile(first, last, **user_info): # 兩個星號表示可容納任意數量的空字典
  '''建立一個空字典,包含我有關的我的信息'''
  profile = {}
  profile['first_name'] = first
  profile['last_name'] = last
  for key, value in user_info.items():
    profile[key] = value
  return profile

user_profile = build_profile('sun','star',
             location='suzhou',
             age='27',
             filed='planning')
print(user_profile)

# 8-14 汽車
def build_profile(first, last, **car_info):
  '''建立一個空字典,包含咱們知道的有關車的信息'''
  profile = {}
  profile['manufacturer:'] = first
  profile['model:'] = last
  for key, value in car_info.items():
    profile[key] = value
  return profile

car_profile = build_profile('sabaru','outback',
            color='bule',
            two_package=True)
print(car_profile)

 

# 8.6.1 導入整個模塊
'''子文件須要新建_init_.py空文件'''
from Function_library import pizza

'''模塊名稱pizza和函數名make_pizza(),並用句點分隔它們'''
pizza.make_pizza(16,'peppertoni')
pizza.make_pizza(12,'mushrooms', 'green peppers', 'extra cheese')

# 8.6.2 導入特定函數
'''from Function_library.pizza import:導入特定函數'''
from Function_library.pizza import make_pizza
'''顯式地導入函數make_pizza(),無須句點分隔,只需指定名稱'''
make_pizza(16,'peppertoni')
make_pizza(12,'mushrooms', 'green peppers', 'extra cheese')

# 8.6.4 使用as給模塊指定別名
'''from Function_library.pizza import make_=pizza as mp: 指定別名語法'''
from Function_library.pizza import make_pizza as mp

'''無須複雜輸入make_pizza,只需輸入mp便可'''
mp(16,'peppertoni')
mp(12,'mushrooms', 'green peppers', 'extra cheese')

# 8.6.5 導入模塊中的全部函數
'''使用(*)號運算符可以讓python導入模塊中的全部函數:'''
from Function_library.pizza import *

make_pizza(16,'peppertoni')
make_pizza(12,'mushrooms', 'green peppers', 'extra cheese')

# 指定函數描述性名稱幫助你和別人明白代碼
# 每一個函數都有包含簡要地闡述其功能的註釋,緊跟函數定義後面

相關文章
相關標籤/搜索