高级编程技术作业(七、八)

理解:对于我而言,函数中任意数量参数和任意数量的关键字实参是比较陌生的。但只要弄清楚任意数量参数相当于元组,任意数量的关键字实参相当于字典,以及两者的表示方式、使用方法,就不难区别。


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

num = int(input("Please input an integer to verify if it can be divided exactly by 10:"))
if num%10==0:
	print("Yes!")
else:
	print("No!")

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

·使用变量active 来控制循环结束的时机。

prompt = "Hello, please input the age of customer to get know the corresponding price for him. You can input 'quit' to end."
print(prompt)
active = True
while active:
    str = input()
    if str == 'quit':
        active = False
        print("Bye~")
    elif int(str) < 3:
        print("Free!")
    elif int(str) <= 12 and int(str) >= 3:
        print("10 dollars.")
    else:
        print("15 dollars.")

·使用break 语句在用户输入’quit’ 时退出循环。

prompt = "Hello, please input the age of customer to get know the corresponding price for him. You can input 'quit' to end."
print(prompt)
while True:
    str = input()
    if str == 'quit':
        print("Bye~")
        break
    age = int(str)
    if age < 3:
        print("Free!")
    elif age <= 12 and age >= 3:
        print("10 dollars.")
    else:
        print("15 dollars.")

结果:

Hello, please input the age of customer to get know the corresponding price for him. You can input 'quit' to end.
12
10 dollars.
5
10 dollars.
13
15 dollars.
2
Free!
quit
Bye~


7-8熟食店 :创建一个名为sandwich_orders 的列表,在其中包含各种三明治的名字;再创建一个名为finished_sandwiches的空列表。遍历列表sandwich_orders ,对于其中的每种三明治,都打印一条消息,如I made your tuna sandwich ,并将其移到列表finished_sandwiches。所有三明治都制作好后,打印一条消息,将这些三明治列出来。

sandwich_orders = ["Apple","chicken","tuna"]
finished_sandwiches = []
for sandwich in sandwich_orders:
    print("I made your " + sandwich + " sandwich.")
    finished_sandwiches.append(sandwich)
print("\nWe have these sandwiches now.")
for sandwich in finished_sandwiches:
    print(sandwich + " sandwich.")

结果:

I made your Apple sandwich.
I made your chicken sandwich.
I made your tuna sandwich.

We have these sandwiches now.
Apple sandwich.
chicken sandwich.
tuna sandwich.


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

def favorite_book(title):
    print("One of my favorite book is " + title + '.')
    
favorite_book("Moby Dick")


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

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

describe_city('Guangzhou')
describe_city('Shaoyang')
describe_city('New York', 'the United States')

结果:

Guangzhou is in China.
Shaoyang is in China.
New York is in the United States.

8-7专辑 :编写一个名为make_album() 的函数,它创建一个描述音乐专辑的字典。这个函数应接受歌手的名字和专辑名,并返回一个包含这两项信息的字典。使用这个函数创建三个表示不同专辑的字典,并打印每个返回的值,以核实字典正确地存储了专辑的信息。

def make_album(name, title):
    album = {}
    album['name'] = name
    album['title'] = title
    return album

a = make_album("Adele", '21')
b = make_album("Taylor Swift", 'Reputation')
c = make_album("Leslie Cheung", '我')
print(a)
print(b)
print(c)

结果:

{'name': 'Adele', 'title': '21'}
{'name': 'Taylor Swift', 'title': 'Reputation'}
{'name': 'Leslie Cheung', 'title': '我'}

给函数make_album() 添加一个可选形参,以便能够存储专辑包含的歌曲数。如果调用这个函数时指定了歌曲数,就将这个值添加到表示专辑的字典中。调用这个函数,并至少在一次调用中指定专辑包含的歌曲数。

def make_album(name, title, number = ''):
    album = {}
    album['name'] = name
    album['title'] = title
    if number:
        album['number'] = int(number)
    return album

a = make_album("Adele", '21', 14)
b = make_album("Taylor Swift", 'Reputation', 9)
c = make_album("Leslie Cheung", '我')
print(a)
print(b)
print(c)

结果:

{'name': 'Adele', 'title': '21', 'number': 14}
{'name': 'Taylor Swift', 'title': 'Reputation', 'number': 9}
{'name': 'Leslie Cheung', 'title': '我'}

8-9魔术师 :创建一个包含魔术师名字的列表,并将其传递给一个名为show_magicians() 的函数,这个函数打印列表中每个魔术师的名字。

def show_magicians(l):
    for name in l:
        print(name)

list = ['A', 'B', 'C', 'D']
show_magicians(list)

8-12三明治 :编写一个函数,它接受顾客要在三明治中添加的一系列食材。这个函数只有一个形参(它收集函数调用中提供的所有食材),并打印一条消息,对顾客点的三明治进行概述。调用这个函数三次,每次都提供不同数量的实参。

def pizza(*toppings):
    print("A pizza with the following toppings:")
    for topping in toppings:
        print("-"+topping)
    print('\n')

pizza("pepper",'chicken','mushroom')
pizza('cheese','beef')
pizza('green peppers','cheese','chicken','beef')

结果:

A pizza with the following toppings:
-pepper
-chicken
-mushroom


A pizza with the following toppings:
-cheese
-beef


A pizza with the following toppings:
-green peppers
-cheese
-chicken
-beef

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值