您的位置:首页 > 编程语言 > Python开发

Python学习第四周第一次作业——用户输入和while循环

2018-03-31 17:00 751 查看
第四周课后作业,第七章的练习题选几道写一下
7-2 餐馆订位 : 编写一个程序, 询问用户有多少人用餐。 如果超过8人, 就打印一条消息, 指出没有空桌; 否则指出有空桌。
number = input("有多少人需要用餐?")
number = int(number)
if number > 8:
print("没有空桌子了")
else:
print("有空桌子")7-5 电影票 : 有家电影院根据观众的年龄收取不同的票价: 不到3岁的观众免费; 3~12岁的观众为10美元; 超过12岁的观众为15美元。 请编写一个循环, 在其中询问用户的年龄, 并指出其票价。
while True:
age = input("您的年龄是?")
age = int(age)
if age < 3:
print("免费")
elif age > 12:
print("票价为15美元")
else:
print("票价为10美元")7-6 三个出口 : 以另一种方式完成练习7-4或练习7-5, 在程序中采取如下所有做法。在while 循环中使用条件测试来结束循环。使用变量active 来控制循环结束的时机。使用break 语句在用户输入'quit' 时退出循环 。
age = input("您的年龄是?\t退出请输入quit\n")
if age != 'quit':
age = int(age)
active = 0
while age > 0:
if age < 3:
print("免费")
elif age > 12:
print("票价为15美元")
else:
print("票价为10美元")
age = input("您的年龄是?\t退出请输入quit\n")
if age == 'quit':
break
age = int(age)
active = active + 1
if active > 10:
break7-8 熟食店 : 创建一个名为sandwich_orders 的列表, 在其中包含各种三明治的名字; 再创建一个名为finished_sandwiches 的空列表。 遍历列表sandwich_orders , 对于其中的每种三明治, 都打印一条消息, 如I made your tuna sandwich , 并将其移到列表finished_sandwiches 。 所有三明治都制作好后, 打印一条消息, 将这些三明治列出来。
sanwich_orders = ['tuna','pastrami','pork','pastrami','tuna','pastrami']
finished_sanwiches = []
while len(sanwich_orders) != 0:
print("I made your "+ sanwich_orders[0] + " sandwich.")
finished_sanwiches.append(sanwich_orders[0])
sanwich_orders.pop(0)
print(sanwich_orders)
print(finished_sanwiches)
7-9 五香烟熏牛肉(pastrami) 卖完了 : 使用为完成练习7-8而创建的列表sandwich_orders , 并确保'pastrami' 在其中至少出现了三次。 在程序开头附近添加这样的代码: 打印一条消息, 指出熟食店的五香烟熏牛肉卖完了; 再使用一个while 循环将列表sandwich_orders 中的'pastrami' 都删除。 确认最终的列表finished_sandwiches 中不包含'pastrami' 。
sanwich_orders = ['tuna','pastrami','pork','pastrami','beef','pastrami']
finished_sanwiches = []
print("Sorry, all pastrami sanwiches are sold out.")
while 'pastrami' in sanwich_orders:
sanwich_orders.remove('pastrami')
while len(sanwich_orders) != 0:
print("I made your "+ sanwich_orders[0] + " sandwich.")
finished_sanwiches.append(sanwich_orders[0])
sanwich_orders.pop(0)
print(sanwich_orders)
print(finished_sanwiches)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Python学习