第七次作业:
7-3 10的整数倍 :让用户输入一个数字,并指出这个数字是否是10的整数倍。
x = int(input('please input a number: '))
if x % 10 == 0:
print('Yes')
else:
print('No')
7-5 电影票 :有家电影院根据观众的年龄收取不同的票价:不到3岁的观众免费;3~12岁的观众为10美元;超过12岁的观众为15美元。请编写一个循环,在其中询问用户的年龄,并指出其票价。
prompt = "Please input your age. enter 'exit' to exit\n"
while True:
message = input(prompt)
if message == 'exit':
break
else:
age = int(message)
if age <= 3:
print('This movie is free for you')
elif age <= 12:
print('Please pay $10')
else:
print('Please pay $15')
7-10 梦想的度假胜地 :编写一个程序,调查用户梦想的度假胜地。使用类似于“If you could visit one placein the world, where would you go?”的提示,并编写一个打印调查结果的代码块。
responses = {}
while True:
name = input("\nWhat is your name? ")
response = input("If you could visit one placein the world, where would you go? ")
responses[name] = response
repeat = input("Would you like to let another person respond? (yes/ no) ")
if repeat == 'no':
break
print("\n--- Poll Results ---")
for name, response in responses.items():
print(name + " would like to visit " + response + ".")
第八次作业:
8-1 消息 :编写一个名为display_message() 的函数,它打印一个句子,指出你在本章学的是什么。调用这个函数,确认显示的消息正确无误。
def display_message():
print('I am learning function in this section')
display_message()
8-4 大号T恤 :修改函数make_shirt() ,使其在默认情况下制作一件印有字样“I love Python”的大号T恤。调用这个函数来制作如下T恤:一件印有默认字样的大号T恤、一件印有默认字样的中号T恤和一件印有其他字样的T恤(尺码无关紧要)。
def make_shirt(size, message = 'I love Python'):
print('A ' + size + ' T-shirt with world ' + message + ' on it.')
make_shirt('large')
make_shirt('middle')
make_shirt('small', 'Other world')
8-6 城市名 :编写一个名为city_country() 的函数,它接受城市的名称及其所属的国家。这个函数应返回一个格式类似于下面这样的字符串:"Santiago, Chile" 至少使用三个城市-国家对调用这个函数,并打印它返回的值。
def city_country(city_name, country_name):
return country_name.title() + ', ' + city_name.title()
print(city_country('shanghai', 'China'))
print(city_country('guangzhou', 'China'))
print(city_country('zhejiang', 'China'))
8-9 魔术师 :创建一个包含魔术师名字的列表,并将其传递给一个名为show_magicians() 的函数,这个函数打印列表中每个魔术师的名字。
def show_magicians(name_list):
for name in name_list:
print(name)
name_list = ['Alice', 'Bob', 'Charile']
show_magicians(name_list)
8-14 汽车 :编写一个函数,将一辆汽车的信息存储在一个字典中。这个函数总是接受制造商和型号,还接受任意数量的关键字实参。这样调用这个函数:提供必不可少的信息,以及两个名称—值对,如颜色和选装配件。这个函数必须能够像下面这样进行调用:car = make_car('subaru', 'outback', color='blue', tow_package=True)
def make_car(kind, maker, **user_info):
print('--- Car Information ---')
print('Kind: ' + kind)
print('Made from: ' + maker)
for x, y in user_info.items():
print(x + ': ' + str(y))
car = make_car('subaru', 'outback', color='blue', tow_package=True)