您现在的位置是:首页 > 文章详情

《Python编程:从入门到实践》 第7章习题

日期:2018-09-11点击:690
#7-1汽车租赁:编写一个程序,询问用户要租赁什么样的汽车,并打印一条消息, #如“LetmeseeifIcanfindyouaSubaru”。 car = input() print("Let me see if I can find you a" + car + ".") #7-2 餐馆订位:编写一个程序,询问用户有多少人用餐。如果超过8人,就打印一条消息, #指出没有空桌;否则指出有空桌。 message = int(input("请问您们用餐的有几位?")) if message > 8: print("抱歉,8个位置以上的位置没有空余的了。") elif message <= 0: print("就餐人数不能少于一个人。") else: print("您好,还有位置。") #7-3 10的整数倍:让用户输入一个数字,并指出这个数字是否是10的整数倍。 number = int(input("输入一个数字,我会告诉您,这个数字是否是10的倍数")) if number%10 == 0: print("太棒了!您输入的数字是" + str(number) + ",它是10的倍数。") else: print("真遗憾!您输入的数字是" + str(number) + ",它不是10的倍数。") #7-4比萨配料:编写一个循环,提示用户输入一系列的比萨配料,并在用户输入'quit'时 #结束循环。每当用户输入一种配料后,都打印一条消息,说我们会在比萨中添加这种配料 while True: x = input("请添加披萨的配菜!(输入“quit”退出)") if x != 'quit': print("已为您披萨添加:" + x + "。") else: break #7-5电影票:有家电影院根据观众的年龄收取不同的票价:不到3岁的观众免费;3~12岁的 #观众为10美元;超过12岁的观众为15美元。请编写一个循环,在其中询问用户的年龄, #并指出其票价 age = '' active = True while age != 'qiut': age = input("您好,请问您的年龄是多少?") if age == 'qiut': active = False elif int(age) < 0: print("你不是人,请回你的火星去。") elif int(age) < 3: print("您好,您免费哦!") elif int(age) <= 12: print("您好,收费金额为:10元") elif int(age) > 12: print("您好,收费金额为:15元") #7-6三个出口 : 以另一种方式完成练习7-4或练习7-5, 在程序中采取如下所有做法。 age = '' while True: age = input("您好,请问您的年龄是多少?") if age == 'qiut': print("qiut") break elif int(age) < 0: print("你不是人,请回你的火星去。") elif int(age) < 3: print("您好,您免费哦!") elif int(age) <= 12: print("您好,收费金额为:10元") elif int(age) > 12: print("您好,收费金额为:15元") #7-7无限循环:编写一个没完没了的循环,并运行它(要结束该循环,可按Ctrl+C #,也可关闭显示输出的窗口) while True: print("good") #7-8熟食店:创建一个名为sandwich_orders的列表,在其中包含各种三明治的名字;再 #创建一个名为finished_sandwiches的空列表。遍历列表sandwich_orders,对于其中 #的每种三明治,都打印一条消息,如I made your tuna sandwich ,并将其移到列表 #finished_sandwiches。所有三明治都制作好后,打印一条消息,将这些三明治列出来。 sandwich_orders = ['tuna','Cheese','chicken'] finished_sandwiches = [] while sandwich_orders: sandwich = sandwich_orders.pop() print("I made your " + sandwich + "sandwich.") finished_sandwiches.append(sandwich) print("Print out all the sandwiches") #打印出所有三明治。 for sandwich in finished_sandwiches: print(sandwich + "sandwich.") #7-9五香烟熏牛肉(pastrami)卖完了:使用为完成练习7-8而创建的列表sandwich_orders, #并确保'pastrami'在其中至少出现了三次。在程序开头附近添加这样的代码:打印一条消息, #指出熟食店的五香烟熏牛肉卖完了;再使用一个while循环将列表sandwich_orders中的 #'pastrami'都删除。确认最终的列表finished_sandwiches中不包含'pastrami' sandwich_orders = [ 'tuna', 'pastrami', 'chicken', 'pastrami', 'durian', 'pastrami', 'Cheese' ] finished_sandwiches = [] print("Sorry, we're out of pastrami") while sandwich_orders: sandwich = sandwich_orders.pop() if sandwich == 'pastrami': #比7-8多了这两段 continue print("I made your " + sandwich + " sandwich.") finished_sandwiches.append(sandwich) print("Print out all the sandwiches") #打印出所有三明治。 for sandwich in finished_sandwiches: print(sandwich + " sandwich.") #7-10梦想的度假胜地:编写一个程序,调查用户梦想的度假胜地。使用类似于“If you could #visit one place in the world,where would you go?”的提示,并编写一个打印 #调查结果的代码块。 namesCitys = {} verdict = True while verdict: name = input("您好,您叫什么名字?(输入'qiut即结束')") if name == 'qiut': break citys = input("如果你能去到世界上的一个地方,你会去哪里(输入'qiut即结束')") if citys == 'qiut': break namesCitys[name] = citys print(namesCitys)
原文链接:https://yq.aliyun.com/articles/638620
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章