《Python编程:从入门到实践》习题答案——第7章 用户输入和while循环

# 7-1 汽车租赁
car = input("请输入您想要的汽车:")
print(f"Let me see if I can find you a {car}")

# 7-2 餐厅订位
person_num = input("你们有多少人用餐?")
person_num = int(person_num)

if person_num > 8:
    print("很抱歉,没有空桌了。")
else:
    print("欢迎订餐,有空桌。")

# 7-3 10的倍数
number = input("请输入一个数字:")
number = int(number)
if (number % 10 == 0) and (number != 0):
    print("这个数是10的倍数")
else:
    print("这个数不是10的倍数")

# 7-4 披萨配料
message = " "
while message != 'quit':
    message = input("请你输入您要加的配料:")
    if message != 'quit':
        print("我们会在披萨中加入" + message + "这种配料。")

# 7-5 电影票
prompt = "Enter 'quit' to end"
prompt += "\nPlease enter your age:"

while True:
    age = input(prompt)

    if age == 'quit':
        break
    else:
        if int(age) < 3:
            print("免费")
        elif (int(age) >= 3) and (int(age) <= 12):
            print("您的票价为10元。")
        else:
            print("您的票价为15元。")

# 7-6 三个出口
# 代码同7-5

# 7-7 无限循环
while True:
    print("!!!")

# 7-8 熟食店
sandwich_orders = ['aaa', 'bbb', 'ccc']
finished_sandwiches = []

while sandwich_orders:
    current_order = sandwich_orders.pop()
    print(f"I made your {current_order} sandwich.")
    finished_sandwiches.append(current_order)

print(f"\nfinished_sandwiches:")
for finished_sandwich in finished_sandwiches:
    print(finished_sandwich)

# 7-9 五香烟熏牛肉(pastrami)卖完了
sandwich_orders = ['pastrami', 'aaa','pastrami', 'bbb', 'ccc','pastrami']
print(sandwich_orders)

while 'pastrami' in sandwich_orders:
    sandwich_orders.remove('pastrami')
print(sandwich_orders)

# 7-10 梦想的度假圣地
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?(y/n)")
    if repeat == 'n':
        polling_active = False

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




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值