美文网首页
第四次作业(3.26/3.28)

第四次作业(3.26/3.28)

作者: Pessimist_34ad | 来源:发表于2018-03-30 00:59 被阅读0次

第七章

7-2 餐馆订位 :编写一个程序,询问用户有多少人用餐。如果超过8人,就打印一条消息,指出没有空桌;否则指出有空桌。

people = int(input("How many people?\n"))
if people>8:
    print("No more vacant table.")
else:
    print("There are vacant tables.")

输出结果:

How many people?
9
No more vacant table.
How many people?
4
There are vacant tables.

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

n = int(input("Please input a number:"))
if n % 10 == 0:
    print("%d is multiples of 10" % n)
else:
    print("%d is not a multiples of 10" % 10)

输出结果:

Please input a number:20
20 is multiples of 10
Please input a number:12
12 is not multiples of 10

7-4 比萨配料 :编写一个循环,提示用户输入一系列的比萨配料,并在用户输入’quit’ 时结束循环。每当用户输入一种配料后,都打印一条消息,说我们会在比萨中添加这种配料。

seasoning = input("Input the seasoning:")
while True:
    if seasoning=='quit':
        break
    print("%s will be added to our pizza!" % seasoning)
    seasoning = input("Input the seasoning:")

输出结果:

Input the seasoning:salt
salt will be added to our pizza!
Input the seasoning:sugar
sugar will be added to our pizza!
Input the seasoning:quit

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

while True:
    age = int(input("How old are you?"))
    if age < 0:
        print("System quits")
        break
    if (age < 3):
        print("You are free to the cinema!")
    elif (age >= 3 and age < 12):
        print("Ticket price is $10.")
    else:
        print("Ticket price is $12")

输出结果:

How old are you?2
You are free to the cinema!
How old are you?3
Ticket price is $10.
How old are you?15
Ticket price is $12.

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

sandwich_orders = ['English sandwiches', 'Hamburger', 'Doner sandwich']
finished_sandwiches = []
while len(sandwich_orders) != 0:
    s = sandwich_orders.pop()
    print("I make a %s" % s)
    finished_sandwiches.append(s)
print ("%s have been made" % ", ".join(finished_sandwiches))

输出结果

I make a Doner sandwich
I make a Hamburger
I make a English sandwiches
Doner sandwich, Hamburger, English sandwiches have been made

第八章作业

8-1 消息 :编写一个名为display_message() 的函数,它打印一个句子,指出你在本章学的是什么。调用这个函数,确认显示的消息正确无误。

def display(sentence):
    print(sentence)
s = "Function"
display(s)

8-3 T恤 :编写一个名为make_shirt() 的函数,它接受一个尺码以及要印到T恤上的字样。这个函数应打印一个句子,概要地说明T恤的尺码和字样。使用位置实参调用这个函数来制作一件T恤;再使用关键字实参来调用这个函数。
8-4 大号T恤 :修改函数make_shirt() ,使其在默认情况下制作一件印有字样“I love Python”的大号T恤。调用这个函数来制作如下T恤:一件印有默认字样的大号T恤、一件印有默认字样的中号T恤和一件印有其他字样的T恤(尺码无关紧要)。

def make_shirt(size = "large", letter = "I love Python"):
    print("The T-shirt's size is %s with \"%s\" on it." % (size, letter))

make_shirt()
make_shirt(size = "middle")
make_shirt(size = "small", letter = "I love php")

输出结果:

The T-shirt's size is large with "I love Python" on it.
The T-shirt's size is middle with "I love Python" on it.
The T-shirt's size is small with "I love php" on it.

8-7 专辑 :编写一个名为make_album() 的函数,它创建一个描述音乐专辑的字典。这个函数应接受歌手的名字和专辑名,并返回一个包含这两项信息的字典。使用这个函数创建三个表示不同专辑的字典,并打印每个返回的值,以核实字典正确地存储了专辑的信息。
给函数make_album() 添加一个可选形参,以便能够存储专辑包含的歌曲数。如果调用这个函数时指定了歌曲数,就将这个值添加到表示专辑的字典中。调用这个函数,并至少在一次调用中指定专辑包含的歌曲数。

def make_album(singer, album, number=None):
    ret = {
        'singer': singer,
        'album': album,
    }
    if number != None:
        ret['number'] = number
    return ret

print(make_album('singer1', 'album1'))
print(make_album('singer2', 'album2'))
print(make_album('Touma Kazusa', 'White Album', 5))

输出结果:

{'singer': 'singer1', 'album': 'album1'}
{'singer': 'singer2', 'album': 'album2'}
{'number': 5, 'singer': 'Touma Kazusa', 'album': 'White Album'}

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

def show_magicians(myList):
    for i in myList:
        print(i)

magicians = ['A', 'B', 'C']
show_magicians(magicians)

相关文章

  • 日本樱花3月(3)

    3.26 3.27 3.28 3.29

  • 第四次作业(3.26/3.28)

    第七章 7-2 餐馆订位 :编写一个程序,询问用户有多少人用餐。如果超过8人,就打印一条消息,指出没有空桌;否则指...

  • 3.26高佣打字单A

    佣金:40r 发单时间:3.26 交单时间:3.28上午8:00之前

  • 2021-04-03

    合肥第六届生命成长旅程班-----原生家庭2021.3.26-3.28 导师:许盛源 3.26日怀着激动和熟悉的心...

  • 2018年26班3.28作业汇总

    2018年26班3.28作业汇总

  • ns3使我哭泣

    小白试图装ns3 3.28版本一直报wifi module 错误 退回3.26就安装成功了 感觉是版本问题?(对的...

  • 鄂尔多斯-宫殿之城

    3.26日 向西,去往下一站 - 宫殿之城鄂尔多斯。 3.27日 见证下一个华德福学校的诞生。 3.28日 社区中...

  • 贱贱的健身成长笔记1

    卧推: 7.5kg,6RM,6组力竭 硬拉: 30公斤,8rm 3.26 卧推:10公斤,6rm,4组 3.28 ...

  • 3.26作业

  • 3.28作业

    感谢老师的分享。 这节课的重点是NVC的另2个要素:需要,请求。老师分享了一个小猴子的实验,他们会去铁丝猴妈妈那里...

网友评论

      本文标题:第四次作业(3.26/3.28)

      本文链接:https://www.haomeiwen.com/subject/hvrvcftx.html