Python习题六---Python编程:从入门到实践7.1~7.10

汽车租赁

7-1 汽车租赁 :编写一个程序,询问用户要租赁什么样的汽车,并打印一条消息,如“LetmeseeifIcan find you a Subaru”。

message=input("What kind of car do you want to rent?")
print('Let me see if I can find you a '+message)
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py
What kind of car do you want to rent?RR
Let me see if I can find you a RR

Process finished with exit code 0

餐馆订位

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

num=input('How many people will come to dinner?')
num=int(num)
if num>8:
    print("There are vacent tables")
else:
    print("There are enough tables")
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py
How many people will come to dinner?8
There are enough tables

Process finished with exit code 0

披萨配料

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

prompt = "\nPlease enter pizza toppings:"
prompt += "\nEnter 'quit' to end the program. "
message = " "
while message != 'quit':
     message = input(prompt)
     if message != 'quit':
       print('We will add '+message+' to the pizza')
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py

Please enter pizza toppings:
Enter 'quit' to end the program. aa
We will add aa to the pizza

Please enter pizza toppings:
Enter 'quit' to end the program. bb
We will add bb to the pizza

Please enter pizza toppings:
Enter 'quit' to end the program. quit

Process finished with exit code 0

电影票

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

age = "\ta.您在下方输入你的年龄选择购票;\n"
age += "\n\tb.年输入quit可以退出购票界面;\n"
age += "\n\tc.请输入您的年龄:"
ticket = ""
while ticket != quit:
    ticket = input(age)
    if ticket== 'quit':
       break
    elif int(ticket) < 3:
        print("\n\t小于三岁的免费观影;\n")
    elif int(ticket) <= 12:
        print("\n\t您的票价是10元;\n")
    else:
        print("\n\t您的票价是15元\n")

D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py
	a.您在下方输入你的年龄选择购票;

	b.年输入quit可以退出购票界面;

	c.请输入您的年龄:18

	您的票价是15元

	a.您在下方输入你的年龄选择购票;

	b.年输入quit可以退出购票界面;

	c.请输入您的年龄:quit

Process finished with exit code 0

三个出口

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

age = "\ta.您在下方输入你的年龄选择购票;\n"
age += "\n\tb.年输入quit可以退出购票界面;\n"
age += "\n\tc.请输入您的年龄:"
active=True
while active:
    age=input(age)
    if age=='quit':
        print('您已退出系统')
        break
    else:
        age=int(age)
        if age>0 and age<=3:
            print("\n\t小于三岁的免费观影;\n")
        elif age<=12:
            print("\n\t您的票价是10元;\n")
        else:
            print("\n\t您的票价是15元\n")
            active=False
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py
	a.您在下方输入你的年龄选择购票;

	b.年输入quit可以退出购票界面;

	c.请输入您的年龄:18

	您的票价是15元


Process finished with exit code 0

无限循环

7-7 无限循环 :编写一个没完没了的循环,并运行它(要结束该循环,可按Ctrl+C,也可关闭显示输出的窗口)。

age=2
while age>=2:
    print('j')

熟食店

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

sandwich_orders=['aa','bb','cc']
finished_sandwiches=[]
while sandwich_orders:
    i=sandwich_orders.pop()
    print('I made your '+i+' sandwich')
    finished_sandwiches.append(i)

print('已经完成啦')
for i in finished_sandwiches:
   print(i)

sandwich_orders=['aa','bb','cc']
finished_sandwiches=[]
while sandwich_orders:
    i=sandwich_orders.pop()
    print('I made your '+i+' sandwich')
    finished_sandwiches.append(i)

print('已经完成啦')
for i in finished_sandwiches:
   print(i)

知识点 pop()函数

pop()方法用于随机移除一个元素
例如:set.pop()
随机移除一个元素:

fruits = {'apple','banana','cherry'}
x = fruits.pop()
print(x)
print(fruits)
输出为

banana
{'cherry', 'apple'

五香烟熏牛肉

7-9 五香烟熏牛肉 :使用为完成练习7-8而创建的列表sandwich_orders ,并确保’pastrami’ 在其中至少出现了三次。在程序开头附近添加 这样的代码:打印一条消息,指出熟食店的五香烟熏牛肉卖完了;再使用一个while 循环将列表sandwich_orders 中的’pastrami’ 都删除。确认最终的列 表finished_sandwiches 中不包含’pastrami’

sandwich_orders=['aa','pastrami','bb','pastrami','cc','pastrami']
print('The pastrami has been sold out')
while 'pastrami' in sandwich_orders:
    sandwich_orders.remove('pastrami')
    finished_sandwiches=sandwich_orders
print(finished_sandwiches)

D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py
The pastrami has been sold out
['aa', 'bb', 'cc']

Process finished with exit code 0

梦想的度假胜地

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

responses={}
polling_active=True
while polling_active:
    name=input("\nWhat's your name?")
    place=input('\nIf you could visit one placein the world, where would you go?')
    responses[name]=place
    repeat=input('\nWould you like to let another person respond? (yes/ no)')
    if repeat=='no':
        polling_active=False
    print("\n---  Results ---")
    for name,place in responses.items():
        print(name+' would like to visit '+place)
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py

What's your name?jj

If you could visit one placein the world, where would you go?jj

Would you like to let another person respond? (yes/ no)no

---  Results ---
jj would like to visit jj

Process finished with exit code 0

小结

在本章中,你学习了:如何在程序中使用input() 来让用户提供信息;如何处理文本和数字输入,以及如何使用while 循环让程序按用户的要求不断地运行;多种控制while 循环流程的方式:设置活动标志、使用break 语句以及使用continue 语句;如何使用while 循环在列表之间移动元素,以及如何从列表中删除所有包含特定值的元素;如何 结合使用while 循环和字典。 在第8章中,你将学习函数。函数让你能够将程序分成多个很小的部分,其中每部分都负责完成一项具体任务。你可以根据需要调用同一个函数任意次,还可将函数存储在独立的 文件中。使用函数可让你编写的代码效率更高,更容易维护和排除故障,还可在众多不同的程序中重用。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

看星河的兔子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值