第四周作业

7-2 餐馆订位 :

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

nums = int(input("How many people will eat here?"))
if nums >= 8:
    print("There isn't any available tables.")
else :
    print("There is an available table.")

7-3 10的整数倍 :

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

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

7-5 电影票 :

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

temp = "Please input the age:"
active = True
while active:
    ages = int(input(temp))
    if ages<=3:
        print("free")
    elif ages<=12:
        print("10 dollars")
    else:
        print("15 dollars")

7-6 三个出口 :

以另一种方式完成练习7-4或练习7-5,在程序中采取如下所有做法。在while 循环中使用条件测试来结束循环。
使用变量
active 来控制循环结束的时机。
使用
break 语句在用户输入'quit' 时退出循环。 

temp = "Please input the age:"
active = True
while active:
    ages = input(temp)
    if ages == "quit":
        break
    ages = int(ages)
    if ages<=3:
        print("free")
    elif ages<=12:
        print("10 dollars")
    else:
        print("15 dollars")


7-10 梦想的度假胜地 :

编写一个程序,调查用户梦想的度假胜地。使用类似于“If you could visit one place in the world, where would you go?”的提示,并编写一个打印调查结果的代码块。 

responses = {}
polling_active = True
while polling_active:
    name = input("\nWhat is your name? ")
    response = input("If you could visit one place in the world, where would you go?")
    responses[name] = response
    repeat = input("Would you like to let another person respond? (yes/ no) ")
    if repeat == 'no':
        polling_active = False

print("\n--- Poll Results ---")
for name, response in responses.items():
    print(name.title() + " would like to go to " + response + ".")

8-2 喜欢的图书 :

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

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

def book(name):
    print("One of my favorite books is " + name + ".")


book("Alice in Wonderland")

8-5 城市 :

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

def describe_city(city_name,country_name = "China"):
    print(city_name + " is in " + country_name)


describe_city("Guangzhou")
describe_city("Los Angeles", "America")
describe_city("Shenzhen")


8-7 专辑 :

编写一个名为make_album() 的函数,它创建一个描述音乐专辑的字典。这个函数应接受歌手的名字和专辑名,并返回一个包含这两项信息的字典。使

用这个函数创建三个表示不同专辑的字典,并打印每个返回的值,以核实字典正确地存储了专辑的信息。

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

def build(musician_name, album_name, tot=''):
    albums = {'musician_name': musician_name, 'album_name': album_name}
    if tot:
        albums['total_number'] = tot
    return albums


albums = build('Alice', 'lalala')
print(albums)
albums = build('Tom', 'lalala',34)
print(albums)
albums = build('Bob', 'lalala',20)
print(albums)

8-9 魔术师 :

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

def print_names(names):
    for name in names:
        print(name.title())


names = ['hannah', 'ty', 'margot']
print_names(names)


8-13 用户简介 :

复制前面的程序user_profile.py,在其中调用build_profile() 来创建有关你的简介;调用这个函数时,指定你的名和姓,以及三个描述你的键-值对。 

def build_profile(first, last, **user_info):
    profile = {}
    profile['first_name'] = first
    profile['last_name'] = last.title()
    for key, value in user_info.items():
        profile[key] = value
    return profile


user_profile = build_profile('hj', 'zhang', age='10',sex='girl')
print(user_profile)

8-15 打印模型 :

将示例print_models.py中的函数放在另一个名为printing_functions.py的文件中;在print_models.py的开头编写一条import 语句,并修改这个文件以使用导入的函数。 

print_models.py

from printing_functions import print_models as pm
from printing_functions import show_completed_models as scm
unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []
pm(unprinted_designs, completed_models)
scm(completed_models)
printing_functions.py
def print_models(unprinted_designs, completed_models): 
    while unprinted_designs:
        current_design = unprinted_designs.pop()
        print("Printing model: " + current_design) 
        completed_models.append(current_design)


def show_completed_models(completed_models): 
    print("\nThe following models have been printed:")
    for completed_model in completed_models:
        print(completed_model)







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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值