pizza.py

def make_pizzas(size, *toppings):
    """概述要制作的比萨"""
    print('size:' + size)
    print(toppings)


def test_one():
    print('one')


def test_two():
    print('two')


def test_three():
    print('three')

06.fun.py

# 导入函数模块
import pizza
# 导入特定的函数
from pizza import test_one, test_two
# 使用 as 给函数指定别名
from pizza import make_pizzas as mp
# 使用 as 给模块指定别名
import pizza as p

pizza.make_pizzas('16', 'pepperoni')
test_one()
test_two()
mp('16', 'pepperoni')
p.make_pizzas('16', 'pepperoni')


def greet_user(name, year=18):
    # 文档字符串用三引号括起,Python使用它们来生成有关程序中函数的文档
    """显示简单的问候语"""
    print("hello, " + name.title() + ". you're " + str(year) + " years old.")


greet_user('zyt', 20)
greet_user(name='zyt', year=19)
greet_user(year=19, name='zyt')


def get_formatted_name(first_name, middle_name, last_name=''):
    """返回整洁的名字"""
    full_name = first_name + ' ' + middle_name + ' ' + last_name
    return full_name.title()


musician = get_formatted_name('joun', 'lee', 'hooker')
print(musician)

# while True:
#     print('tell me your name: ')
#     first_name = input("first_name: ")
#     if first_name == 'q':
#         break
#     middle_name = input("middle_name: ")
#     if middle_name == 'q':
#         break
#     full_name = get_formatted_name(first_name, middle_name)
#     print(full_name)


def say_hello(names):
    for name in names:
        print('hello, ' + name.title())


say_hello(['zyt', 'zzz', 'yyy'])


def print_models(unprinted_designs, completed_models):
    """
    模板打印每个设计,直到没有未打印的设计为止
    打印每个设计后,都将其移到列表completed_models中
    """
    while unprinted_designs:
        current_design = unprinted_designs.pop()
        completed_models.append(current_design)


def show_completed_models(completed_models):
    """显示打印好的所有模型"""
    for completed_model in completed_models:
        print(completed_model)


unprinted_designs = ['aa', 'bb', 'cc', 'dd']
completed_models = []
print('unprinted_designs为空')
print_models(unprinted_designs, completed_models)
show_completed_models(completed_models)
print(unprinted_designs)
# 切片表示法[:] 创建列表的副本
unprinted_designs = ['aa', 'bb', 'cc', 'dd']
completed_models = []
print('切片表示法[:] ')
print_models(unprinted_designs[:], completed_models)
show_completed_models(completed_models)
print(unprinted_designs)


# *toppings 中的星号让创建一个名为toppings 的空元组,
# 并将收到的所有值都封装到这个元组中
def make_pizza(*toppings):
    """打印顾客点的所有配料"""
    print(toppings)


make_pizza('pepperoni')
make_pizza('mushrooms', 'green peppers', 'extra cheese')


def make_pizzas(size, *toppings):
    """打印顾客点的所有配料"""
    print('size:' + size)
    print(toppings)


make_pizzas('9', 'pepperoni')
make_pizzas('12', 'mushrooms', 'green peppers', 'extra cheese')


# 两个星号创建一个空字典
def build_profile(first, last, **user_info):
    """创建一个字典,其中包含我们知道的关于用户的一切"""
    profile = {}
    profile['first_name'] = first
    profile['last_name'] = last
    for key, val in user_info.items():
        profile[key] = val
    return profile


user_profile = build_profile('z', 'yt', location='jiangsu', field='unknown')
print(user_profile)

# 应给函数指定描述性名称,且只在其中使用小写字母和下划线。
# 描述性名称可帮助你和别人明白代码想要做什么。给模块命名时也应遵循上述约定。
# 每个函数都应包含简要地阐述其功能的注释,该注释应紧跟在函数定义后面,并采用文档字符串格式。
# 文档良好的函数让其他程序员只需阅读文档字符串中的描述就能够使用它:他们完全可以相信代码如描述的那样运行;只要知道函数的名称、需要的实参以及返回值的类型,就能在自己的程序中使用它。
# 给形参指定默认值时,等号两边不要有空格
# 如果程序或模块包含多个函数,可使用两个空行将相邻的函数分开,这样将更容易知道前一个函数在什么地方结束,下一个函数从什么地方开始。
# 所有的import 语句都应放在文件开头,唯一例外的情形是,在文件开头使用了注释来描述整个程序。

 

posted on 2023-02-06 11:32  Zoie_ting  阅读(17)  评论(0编辑  收藏  举报