您的位置:首页 > 编程语言 > Python开发

Python第七,八章练习题 (第四周作业)

2018-04-02 11:17 501 查看
动手试一试

7-3 10的整数倍 :让用户输入一个数字,并指出这个数字是否是10的整数倍。

7-5 电影票 :有家电影院根据观众的年龄收取不同的票价:不到3岁的观众免费;3~12岁的观众为10美元;超过12岁的观众为15美元。请编写一个循环,在其中询问用

户的年龄,并指出其票价。

7-8 熟食店 :创建一个名为sandwich_orders 的列表,在其中包含各种三明治的名字;再创建一个名为finished_sandwiches 的空列表。遍历列

表sandwich_orders ,对于其中的每种三明治,都打印一条消息,如I made your tuna sandwich ,并将其移到列表finished_sandwiches 。所有三明

治都制作好后,打印一条消息,将这些三明治列出来。

7.3

num=int(input())
if num%10 == 0:
print("10的倍数")
else:
print("不是10的倍数")




7.5

num=0
while True :
print("input age,-1 to exit")
num=int(input())
if num == -1 :break
if num<3 :
print("Free")
elif 12>=num>=3 :
print("10 dollors")
else :
print("15 dollors")




7.8

sandwich_orders=["a sandwich","b sandwich","c sandwich"]
finished_sandwiches=[]
while len(sandwich_orders) > 0 :
print("I make your "+sandwich_orders[0])
finished_sandwiches.append(sandwich_orders[0])
sandwich_orders.remove(sandwich_orders[0])
for i in finished_sandwiches:
print("Finish "+i)




动手试一试

8-2 喜欢的图书 :编写一个名为favorite_book() 的函数,其中包含一个名为title 的形参。这个函数打印一条消息,如One of my favorite books is

Alice in Wonderland 。调用这个函数,并将一本图书的名称作为实参传递给它。

8-5 城市 :编写一个名为describe_city() 的函数,它接受一座城市的名字以及该城市所属的国家。这个函数应打印一个简单的句子,如Reykjavik is in

Iceland 。给用于存储国家的形参指定默认值。为三座不同的城市调用这个函数,且其中至少有一座城市不属于默认国家。

8-6 城市名 :编写一个名为city_country() 的函数,它接受城市的名称及其所属的国家。这个函数应返回一个格式类似于下面这样的字符串:

“Santiago, Chile”

8.2

def favorite_book(title):
print("One of my favorite books is "+title)
favorite_book("Alice in Wonderland")




8.5

def describe_city(city,country='China'):
print(city+" is in "+country)

describe_city("Guangzhou")
describe_city("Shanghai")
describe_city("Paris","France")




8.6

def city_country(city,country):
return city+","+country

print(city_country("Guangzhou","China"))
print(city_country("London","England"))


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: