Viết một hàm tên là
The Abstract Wild is one of my favorite books.
1 để in ra một câu cho mọi người biết bạn đang học gì trong chương này. Gọi chức năng và đảm bảo thông báo hiển thị chính xácdef display_message[]:
"""Display a message about what I'm learning."""
msg = "I'm learning to store code in functions."
print[msg]
display_message[]
đầu ra
I'm learning to store code in functions.
8-2. Sách yêu thích
Viết một hàm có tên là
The Abstract Wild is one of my favorite books.
2 chấp nhận một tham số, The Abstract Wild is one of my favorite books.
3. Hàm sẽ in một thông báo, chẳng hạn như The Abstract Wild is one of my favorite books.
4 Gọi hàm, đảm bảo bao gồm tên sách làm đối số trong lệnh gọi hàmdef favorite_book[title]:
"""Display a message about someone's favorite book."""
print[f"{title} is one of my favorite books."]
favorite_book['The Abstract Wild']
đầu ra
The Abstract Wild is one of my favorite books.
8-3. Áo thun
Viết hàm có tên là
The Abstract Wild is one of my favorite books.
5 chấp nhận kích thước và nội dung của thông báo sẽ được in trên áo. Hàm sẽ in một câu tóm tắt kích cỡ của áo và thông báo được in trên đóGọi hàm một lần bằng cách sử dụng đối số vị trí để tạo áo sơ mi. Gọi hàm lần thứ hai bằng các đối số từ khóa
def make_shirt[size, message]:
"""Summarize the shirt that's going to be made."""
print[f"\nI'm going to make a {size} t-shirt."]
print[f'It will say, "{message}"']
make_shirt['large', 'I love Python!']
make_shirt[message="Readability counts.", size='medium']
đầu ra
def display_message[]:
"""Display a message about what I'm learning."""
msg = "I'm learning to store code in functions."
print[msg]
display_message[]
08-4. áo sơ mi lớn
Sửa đổi hàm
The Abstract Wild is one of my favorite books.
5 để mặc định áo sơ mi có kích thước lớn với thông báo có nội dung I love Python. Tạo áo cỡ lớn và áo cỡ vừa với thông báo mặc định và áo cỡ bất kỳ với thông báo khácdef display_message[]:
"""Display a message about what I'm learning."""
msg = "I'm learning to store code in functions."
print[msg]
display_message[]
2đầu ra
def display_message[]:
"""Display a message about what I'm learning."""
msg = "I'm learning to store code in functions."
print[msg]
display_message[]
38-5. Các thành phố
Viết một hàm có tên là
The Abstract Wild is one of my favorite books.
7 chấp nhận tên của một thành phố và quốc gia của nó. Hàm sẽ in ra một câu đơn giản, chẳng hạn như The Abstract Wild is one of my favorite books.
8 Đặt giá trị mặc định cho tham số cho quốc gia. Gọi chức năng của bạn cho ba thành phố khác nhau, ít nhất một trong số đó không thuộc quốc gia mặc địnhdef display_message[]:
"""Display a message about what I'm learning."""
msg = "I'm learning to store code in functions."
print[msg]
display_message[]
6đầu ra
def display_message[]:
"""Display a message about what I'm learning."""
msg = "I'm learning to store code in functions."
print[msg]
display_message[]
78-6. tên thành phố
Viết một hàm có tên là
The Abstract Wild is one of my favorite books.
9 lấy tên của một thành phố và quốc gia của nó. Hàm sẽ trả về một chuỗi có định dạng như thế này“Santiago, Chile”
Gọi hàm của bạn với ít nhất ba cặp thành phố-quốc gia và in giá trị được trả về
I'm learning to store code in functions.
0đầu ra
I'm learning to store code in functions.
18-7. anbom
Viết một hàm có tên là
def make_shirt[size, message]:
"""Summarize the shirt that's going to be made."""
print[f"\nI'm going to make a {size} t-shirt."]
print[f'It will say, "{message}"']
make_shirt['large', 'I love Python!']
make_shirt[message="Readability counts.", size='medium']
0 để xây dựng một từ điển mô tả một album nhạc. Hàm này sẽ lấy tên nghệ sĩ và tiêu đề album và nó sẽ trả về một từ điển chứa hai mẩu thông tin này. Sử dụng chức năng để tạo ba từ điển đại diện cho các album khác nhau. In từng giá trị trả về để cho biết rằng các từ điển đang lưu trữ thông tin anbom một cách chính xácThêm tham số tùy chọn vào
def make_shirt[size, message]:
"""Summarize the shirt that's going to be made."""
print[f"\nI'm going to make a {size} t-shirt."]
print[f'It will say, "{message}"']
make_shirt['large', 'I love Python!']
make_shirt[message="Readability counts.", size='medium']
0 cho phép bạn lưu trữ số lượng bản nhạc trong album. Nếu dòng gọi bao gồm một giá trị cho số lượng bản nhạc, hãy thêm giá trị đó vào từ điển của album. Thực hiện ít nhất một lệnh gọi hàm mới bao gồm số lượng bản nhạc trong albumphiên bản đơn giản
I'm learning to store code in functions.
2đầu ra
I'm learning to store code in functions.
3Với bài hát
I'm learning to store code in functions.
4đầu ra
I'm learning to store code in functions.
58-8. Album người dùng
Bắt đầu với chương trình của bạn từ Bài tập 8-7. Viết vòng lặp
def make_shirt[size, message]:
"""Summarize the shirt that's going to be made."""
print[f"\nI'm going to make a {size} t-shirt."]
print[f'It will say, "{message}"']
make_shirt['large', 'I love Python!']
make_shirt[message="Readability counts.", size='medium']
2 cho phép người dùng nhập nghệ sĩ và tiêu đề của album. Khi bạn có thông tin đó, hãy gọi def make_shirt[size, message]:
"""Summarize the shirt that's going to be made."""
print[f"\nI'm going to make a {size} t-shirt."]
print[f'It will say, "{message}"']
make_shirt['large', 'I love Python!']
make_shirt[message="Readability counts.", size='medium']
0 với đầu vào của người dùng và in từ điển đã tạo. Đảm bảo bao gồm giá trị thoát trong vòng lặp def make_shirt[size, message]:
"""Summarize the shirt that's going to be made."""
print[f"\nI'm going to make a {size} t-shirt."]
print[f'It will say, "{message}"']
make_shirt['large', 'I love Python!']
make_shirt[message="Readability counts.", size='medium']
2I'm learning to store code in functions.
6đầu ra
I'm learning to store code in functions.
78-9. tin nhắn
Tạo một danh sách chứa một loạt các tin nhắn văn bản ngắn. Chuyển danh sách tới một hàm có tên là
def make_shirt[size, message]:
"""Summarize the shirt that's going to be made."""
print[f"\nI'm going to make a {size} t-shirt."]
print[f'It will say, "{message}"']
make_shirt['large', 'I love Python!']
make_shirt[message="Readability counts.", size='medium']
5, hàm này sẽ in từng tin nhắn văn bảnI'm learning to store code in functions.
8đầu ra
I'm learning to store code in functions.
98-10. Gửi tin nhắn
Bắt đầu với một bản sao chương trình của bạn từ Bài tập 8-9. Viết một hàm có tên là
def make_shirt[size, message]:
"""Summarize the shirt that's going to be made."""
print[f"\nI'm going to make a {size} t-shirt."]
print[f'It will say, "{message}"']
make_shirt['large', 'I love Python!']
make_shirt[message="Readability counts.", size='medium']
6 để in từng tin nhắn văn bản và di chuyển từng tin nhắn vào một danh sách mới có tên là def make_shirt[size, message]:
"""Summarize the shirt that's going to be made."""
print[f"\nI'm going to make a {size} t-shirt."]
print[f'It will say, "{message}"']
make_shirt['large', 'I love Python!']
make_shirt[message="Readability counts.", size='medium']
7 khi nó được in. Sau khi gọi chức năng, hãy in cả hai danh sách của bạn để đảm bảo thư đã được di chuyển chính xácdef favorite_book[title]:
"""Display a message about someone's favorite book."""
print[f"{title} is one of my favorite books."]
favorite_book['The Abstract Wild']
0đầu ra
def favorite_book[title]:
"""Display a message about someone's favorite book."""
print[f"{title} is one of my favorite books."]
favorite_book['The Abstract Wild']
18-11. Tin nhắn đã lưu trữ
Bắt đầu với công việc của bạn từ Bài tập 8-10. Gọi hàm send_messages[] với một bản sao của danh sách các tin nhắn. Sau khi gọi hàm, hãy in cả hai danh sách của bạn để cho biết rằng danh sách gốc đã giữ lại các thông báo của nó
def favorite_book[title]:
"""Display a message about someone's favorite book."""
print[f"{title} is one of my favorite books."]
favorite_book['The Abstract Wild']
2đầu ra
def favorite_book[title]:
"""Display a message about someone's favorite book."""
print[f"{title} is one of my favorite books."]
favorite_book['The Abstract Wild']
38-12. bánh mì kẹp
Viết hàm chấp nhận danh sách các món mà một người muốn có trên bánh sandwich. Hàm phải có một tham số thu thập nhiều mục như lệnh gọi hàm cung cấp và nó sẽ in bản tóm tắt về sandiwch đang được đặt hàng. Gọi hàm ba lần, mỗi lần sử dụng một số đối số khác nhau
def favorite_book[title]:
"""Display a message about someone's favorite book."""
print[f"{title} is one of my favorite books."]
favorite_book['The Abstract Wild']
4đầu ra
def favorite_book[title]:
"""Display a message about someone's favorite book."""
print[f"{title} is one of my favorite books."]
favorite_book['The Abstract Wild']
58-14. ô tô
Viết hàm lưu thông tin về ô tô vào từ điển. chức năng phải luôn nhận được nhà sản xuất và tên kiểu máy. Sau đó, nó sẽ chấp nhận một số đối số từ khóa tùy ý. Gọi hàm với thông tin bắt buộc và hai cặp tên-giá trị khác, chẳng hạn như màu hoặc tính năng tùy chọn. Chức năng của bạn sẽ hoạt động cho một cuộc gọi như thế này
def make_shirt[size, message]:
"""Summarize the shirt that's going to be made."""
print[f"\nI'm going to make a {size} t-shirt."]
print[f'It will say, "{message}"']
make_shirt['large', 'I love Python!']
make_shirt[message="Readability counts.", size='medium']
8In từ điển được trả về để đảm bảo tất cả thông tin được lưu trữ chính xác
def favorite_book[title]:
"""Display a message about someone's favorite book."""
print[f"{title} is one of my favorite books."]
favorite_book['The Abstract Wild']
6đầu ra
def favorite_book[title]:
"""Display a message about someone's favorite book."""
print[f"{title} is one of my favorite books."]
favorite_book['The Abstract Wild']
78-15. Mô hình in
Đặt các chức năng cho ví dụ print_models. py trong một tệp riêng có tên là print_functions. py. Viết câu lệnh
def make_shirt[size, message]:
"""Summarize the shirt that's going to be made."""
print[f"\nI'm going to make a {size} t-shirt."]
print[f'It will say, "{message}"']
make_shirt['large', 'I love Python!']
make_shirt[message="Readability counts.", size='medium']
9 ở đầu print_models. py và sửa đổi tệp để sử dụng các hàm đã nhập