#8-1 消息
def display_message():
"""指出你在本章學的是什么"""
print("學習定義函數")
display_message()
python 類,#8-2 喜歡的圖書
def favorite_book(title):
"""喜歡的圖書"""
print("\nOne of my favorite books is " + title + ".")
favorite_book('The old man and the sea')
#8-3 T恤
qpython,def make_shirt(T_size, T_type):
print("\nThe T-shirt's size is: " + T_size)
print("The T-shirt's type is: " + T_type)
make_shirt('39', 'freestyle')
make_shirt(T_size='39', T_type='freestyle')
#8-4 大號T恤
python和java,def make_shirt(T_size, T_type='I love python'):
print("\nThe T-shirt's size is: " + T_size)
print("The T-shirt's type is: " + T_type)
make_shirt('45')
make_shirt('40')
make_shirt('39', 'Beliver')
python為什么叫爬蟲,#8-5 城市
def describe_city(name, country='China'):
print("\n" + name.title() +" is in " + country.title())
describe_city('hangzhou')
describe_city('changsha')
describe_city('new york', 'america')
python編程100例,#8-7 專輯
def make_album(singer, album_name, song_number=''):
"""描述音樂專輯的字典"""
album = {'singer': singer, 'album name': album_name}
if song_number:
album['song number'] = song_number
return album
musician = make_album('jay', 'fantasy')
print(musician)
musician = make_album('jay', 'fantasy', '10')
print(musician)
#8-8 用戶的專輯
while True:
print("\nPlease enter the information of album: ")
print("(enter 'quit' any time to quit)")
s = input("singer: ")
if s == 'quit':
break
a_n = input("album name: ")
if a_n == 'quit':
break
musician = make_album(s, a_n)
print(musician)
#8-9 魔術師
def show_magicians(magician_names):
"""顯示全部魔術師名字"""
for magician_name in magician_names:
print(magician_name)
magician_names = ['magician_1', 'magician_2', 'magician_3']
show_magicians(magician_names)
#8-10 了不得的魔術師
def make_great(magician_names):
"""修改魔術師列表,加the great"""
n = 0
while n < len(magician_names):
magician_names[n] = "the Great " + magician_names[n]
n += 1
magician_names = ['magician_1', 'magician_2', 'magician_3']
make_great(magician_names)
show_magicians(magician_names)
#8-11 不變的魔術師
def make_great(magician_names):
"""修改魔術師列表,加the great"""
n = 0
while n < len(magician_names):
magician_names[n] = "the Great " + magician_names[n]
n += 1
return magician_names
magician_names = ['magician_1', 'magician_2', 'magician_3']
great_magician_names = make_great(magician_names[:])
show_magicians(magician_names)
show_magicians(great_magician_names)
#8-12 三明治
def make_sandwich(*toppings):
"""添加食材"""
print(toppings)
make_sandwich('mushrooms')
make_sandwich('mushrooms', 'green peppers')
make_sandwich('mushrooms', 'green peppers', 'extra cheese')
#8-13 用戶簡介
def build_profile(first, last, **user_info):
"""建立一個字典,其中包含咱們知道的有關用戶的一切"""
profile = {}
profile['first'] = first
profile['last'] = last
for k, v in user_info.items():
profile[k] = v
return profile
user_profile = build_profile('albert', 'einstein',
location='princeton',
field='physics',
IQ='200')
print(user_profile)
#8-14 汽車
def make_car(manufacturer, model, **other):
"""建立一個字典,其中包含汽車的信息"""
car = {}
car['manufacturer'] = manufacturer
car['model'] = model
for k,v in other.items():
car[k] = v
return car
car = make_car('subaru', 'outback', color='bule', tow_package=True)
print(car)
#8-15 打印模型
#8.6.1 導入整個模塊
import printing_functions
unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []
printing_functions.print_models(unprinted_designs, completed_models)
printing_functions.show_completed_models(completed_models)
#8.6.2 導入特定的函數
from printing_functions import print_models, show_completed_models
print_models(unprinted_designs, completed_models)
show_completed_models(completed_models)
#8.6.3 使用as給函數指定別名
from printing_functions import print_models as pm, show_completed_models as scm
pm(unprinted_designs, completed_models)
scm(completed_models)
#8.6.4 使用as給模塊指定別名
import printing_functions as pf
pf.print_models(unprinted_designs, completed_models)
pf.show_completed_models(completed_models)
#8.6.5 導入模塊中的全部函數(不建議使用)
# ~ from printing_functions import *
#8-16 導入(同上)
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态