7-2 餐馆定位
i = int(input("请问有多少人用餐? "))
while i > 0:
if i > 8:
print("目前没有空桌")
else:
print("目前有空桌")
i = int(input("请问有多少人用餐? "))
输出结果是:
请问有多少人用餐? 5
目前有空桌
请问有多少人用餐? 10
目前没有空桌
7-3 10的整倍数
i = int(input("请输入一个数字: "))
while i > 0:
if i % 10 == 0:
print(str(i)+"是10的"+str(int(i/10))+"倍")
else:
print(str(i)+"不是10的整数倍")
i = int(input("请输入一个数字: "))
输出结果是:
请输入一个数字: 100
100是10的10倍
请输入一个数字: 59
59不是10的整数倍
7-5 电影票
i = int(input("请输入观众的年龄: "))
while i >= 0:
if i < 3:
print("该观众免费")
elif i <= 12:
print("该观众需要支付10美元")
else:
print("该观众需要支付15美元")
i = int(input("请输入观众的年龄: "))
输出结果是:
请输入观众的年龄: 15
该观众需要支付15美元
请输入观众的年龄: 9
该观众需要支付10美元
请输入观众的年龄: 0
该观众免费
7-6 三个出口
active = True
while active:
i = input("请输入观众的年龄: ")
if i == "quit":
break
elif int(i) < 3:
print("该观众免费")
elif int(i) <= 12:
print("该观众需要支付10美元")
else:
print("该观众需要支付15美元")
输出结果是:
请输入观众的年龄: 15
该观众需要支付15美元
请输入观众的年龄: 9
该观众需要支付10美元
请输入观众的年龄: 0
该观众免费
请输入观众的年龄: quit
7-8 熟食店
sandwich_orders = ['egg mayo','prawn mayo','tuna mayo','bacon lettuce tomato','ham & cheese','roast chicken']
finished_sandwiches = []
while sandwich_orders:
current = sandwich_orders.pop()
print("I made your "+current+" sandwich")
finished_sandwiches.append(current)
print("\nI have these sandwiches:")
for sandwich in finished_sandwiches:
print(sandwich+" sandwich")
输出结果是:
I made your roast chicken sandwich
I made your ham & cheese sandwich
I made your bacon lettuce tomato sandwich
I made your tuna mayo sandwich
I made your prawn mayo sandwich
I made your egg mayo sandwich
I have these sandwiches:
roast chicken sandwich
ham & cheese sandwich
bacon lettuce tomato sandwich
tuna mayo sandwich
prawn mayo sandwich
egg mayo sandwich
7-9 五香烟熏牛肉卖光了
sandwich_orders = ['egg mayo','prawn mayo','pastrami','tuna mayo','bacon lettuce tomato'
,'pastrami','ham & cheese','pastrami','roast chicken']
print("Pastrami has been sold out.")
finished_sandwiches = []
while 'pastrami' in sandwich_orders:
sandwich_orders.remove('pastrami')
while sandwich_orders:
current = sandwich_orders.pop()
finished_sandwiches.append(current)
print("\nI have these sandwiches:")
for sandwich in finished_sandwiches:
print(sandwich+" sandwich")
输出结果是:
Pastrami has been sold out.
I have these sandwiches:
roast chicken sandwich
ham & cheese sandwich
bacon lettuce tomato sandwich
tuna mayo sandwich
prawn mayo sandwich
egg mayo sandwich