Python编程:从入门到实践 (第八章习题)

本文档展示了Python编程中关于函数的多个练习,包括定义函数、传递参数、返回值以及使用默认参数等。通过示例,如`display_message()`、`favorite_book()`、`make_shirt()`等,演示了如何编写和调用函数,以及如何处理不同类型的参数。此外,还涉及到了函数的导入和使用。
#8-1 消息:编写一个名为 display_message()的函数,它打印一个句子,指出你在本章学的是什么。
# 调用这个函数,确认显示的消息正确无误。
def display_message():
   print("In the chapter 8, we will learn how to define a function!")

display_message()

#8-2 喜欢的图书:编写一个名为 favorite_book()的函数,其中包含一个名为 title的形参。
# 这个函数打印一条消息,如 One of my favorite books is Alice in Wonderland。
# 调用这个函数,并将一本图书的名称作为实参传递给它。

def favorite_book(title):
   """favorite books"""
   print("One of my favorite books is " +title.title()  +".")

favorite_book("animal farm")

#8-3 T 恤:编写一个名为 make_shirt()的函数,它接受一个尺码以及要印到 T 恤上的字样。
# 这个函数应打印一个句子,概要地说明 T 恤的尺码和字样。
# 使用位置实参调用这个函数来制作一件 T 恤;再使用关键字实参来调用这个函数。


def make_shirt(size,letter_sample):
   """information of size and letter sample"""
   print("\nThis t-shirt is in size " + size.upper() + ", and please print " + letter_sample.upper() + " on it.")


make_shirt("l","i love you")
make_shirt(letter_sample= "love",size="x")

#8-4 大号 T 恤:修改函数 make_shirt(),使其在默认情况下制作一件印有字样“I love  Python”的大号 T 恤。
# 调用这个函数来制作如下 T 恤:
# 一件印有默认字样的大号 T 恤、一件印有默认字样的中号 T 恤和一件印有其他字样的 T 恤(尺码无关紧要)


def make_shirt(size="L",letter_sample="I love Python"):
   """information of size and letter sample"""
   print("\nPlease make a t-shirt is in size " + size.upper() + ", and print " + letter_sample + " on it.")


make_shirt()
make_shirt(size="xl")
make_shirt("m",'Love')

#8-5 城市:编写一个名为 describe_city()的函数,它接受一座城市的名字以及该城市所属的国家。
# 这个函数应打印一个简单的句子,如 Reykjavik is in Iceland。给用于存储国家的形参指定默认值。
# 为三座不同的城市调用这个函数,且其中至少有一座城市不属于默认国家。


def describe_city(city,country='china'):
   print("\n" + city.title() + " is in " + country.title() +".")


describe_city("shanghai")
describe_city("hongkong")
describe_city
Python编程:从入门实践》是一本适合编程初学者的书籍,书中包含大量练习题以帮助读者巩固所学知识。以下是一些章节的练习题答案示例: ### 第2章:变量和简单数据类型 ```python # 2-1 message = "hello" print(message) # 2-2 message = "nihao1" print(message) message = "nihao2" print(message) # 2-3 name = "lihua" print("Hello " + name + ", would you like to learn some Python today") # 2-4 name = "hua Li" print(name + "\n" + name.title() + "\n" + name.upper() + "\n" + name.lower()) # 2-5 print('Albert Einstein once said, "A person who never made a mistake never tried anything new"') # 2-6 famous_person = "Albert Einstein" message = '"A person who never made a mistake never tried anything new"' print(famous_person + " once said," + message) # 2-7 person = "\tli hua\t" print(person + person.rstrip() + person.lstrip() + person.strip()) # 2-8 print(5 + 3) print(10 - 2) print(2 * 4) print(16 / 2) # 2-9 number = 7 print("I like " + str(number)) # 2-10 # 向大家问好 print("hello") # 2-11 import this ``` ### 第3章:列表简介 ```python # 3-1 friends = ['Alice', 'Bob', 'Charlie'] for friend in friends: print(friend) # 3-2 friends = ['Alice', 'Bob', 'Charlie'] for friend in friends: print("Hello " + friend + ", how are you?") # 3-4 guests = ['Alice', 'Bob', 'Charlie'] for guest in guests: print("Dear " + guest + ", you are invited to dinner.") # 3-5 guests = ['Alice', 'Bob', 'Charlie'] unavailable_guest = 'Bob' new_guest = 'David' guests.remove(unavailable_guest) guests.append(new_guest) for guest in guests: print("Dear " + guest + ", you are invited to dinner.") # 3-6 guests = ['Alice', 'Bob', 'Charlie'] new_guests = ['David', 'Eve', 'Frank'] guests.insert(0, new_guests[0]) guests.insert(len(guests) // 2, new_guests[1]) guests.extend(new_guests[2:]) for guest in guests: print("Dear " + guest + ", you are invited to dinner.") ``` ### 第4章:操作列表 ```python # 4-1 pizzas = ['Pepperoni', 'Cheese', 'Veggie'] for pizza in pizzas: print("I like " + pizza + " pizza.") print("I really love pizza!") # 4-2 animals = ['Dog', 'Cat', 'Bird'] for animal in animals: print("A " + animal + " would make a great pet.") print("Any of these animals would make a great pet!") # 4-3 for number in range(1, 21): print(number) # 4-4 for number in range(1, 1000001): print(number) # 4-5 numbers = list(range(1, 1000001)) print(min(numbers)) print(max(numbers)) print(sum(numbers)) # 4-6 for number in range(1, 21, 2): print(number) # 4-7 for number in range(3, 31, 3): print(number) # 4-8 cubes = [number ** 3 for number in range(1, 11)] for cube in cubes: print(cube) # 4-9 cubes = [number ** 3 for number in range(1, 11)] print(cubes) # 4-10 pizzas = ['Pepperoni', 'Cheese', 'Veggie', 'Hawaiian', 'BBQ'] print("The first three items in the list are: " + str(pizzas[:3])) print("Three items from the middle of the list are: " + str(pizzas[1:4])) print("The last three items in the list are: " + str(pizzas[-3:])) # 4-11 my_pizzas = ['Pepperoni', 'Cheese', 'Veggie'] friend_pizzas = my_pizzas[:] my_pizzas.append('Hawaiian') friend_pizzas.append('BBQ') print("My favorite pizzas are: " + str(my_pizzas)) print("My friend's favorite pizzas are: " + str(friend_pizzas)) ``` 这些示例涵盖了书中部分章节的练习题答案,可以帮助读者更好地理解和掌握Python编程的基础知识。希望这些内容对你有所帮助[^1]。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值