Python编程:从入门到实践(第2版)第七章课后题

本文介绍了几个Python编程练习,涉及用户输入验证、条件判断、循环结构,如汽车租赁选择、餐馆订位、数字特性检测、比萨配料输入等,展示了基础的交互式程序设计。
摘要由CSDN通过智能技术生成

第七章

7.1

练习7-1:汽车租赁  编写一个程序,询问用户要租赁什么样的汽车,并打印 一条消息,下面是一个例子。 Let me see if I can find you a Subaru.

car = input("What kind of car would you like to rent? ")
print(f"Let me see if I can find you a {car}.")

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


num_people = int(input("How many people are in your party? "))

if num_people > 8:
    print("Sorry, there are no available tables.")
else:
    print("There are available tables for your party.")

练习7-3:10的整数倍  让用户输入一个数,并指出该数是否是10的整数倍

number = int(input("Enter a number: "))

if number % 10 == 0:
    print(f"{number} is a multiple of 10.")
else:
    print(f"{number} is not a multiple of 10.")

7.2

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

print("Enter the pizza toppings you want to add.")
print("Enter 'quit' when you are finished.")

while True:
    topping = input("Enter a pizza topping: ")
    if topping == 'quit':
        break
    else:
        print(f"We will add {topping} to the pizza.")

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

while True:
    age = input("Enter your age (enter 'quit' to exit): ")
    if age == 'quit':
        break
    age = int(age)
    if age < 3:
        print("You get in for free!")
    elif age <= 12:
        print("The ticket price is $10.")
    else:
        print("The ticket price is $15.")

练习7-6:三种出路
以不同的方式完成练习7-4或练习7-5,在程序中采取如下做法。

在while循环中使用条件测试来结束循环。

print("Enter the pizza toppings you want to add.")
print("Enter 'quit' when you are finished.")

topping = ''
while topping != 'quit':
    topping = input("Enter a pizza topping: ")
    if topping != 'quit':
        print(f"We will add {topping} to the pizza.")

使用变量active来控制循环结束的时机。

print("Enter the pizza toppings you want to add.")
print("Enter 'quit' when you are finished.")

active = True
while active:
    topping = input("Enter a pizza topping: ")
    if topping == 'quit':
        active = False
    else:
        print(f"We will add {topping} to the pizza.")

使用break语句在用户输入'quit'时退出循环。

print("Enter the pizza toppings you want to add.")
print("Enter 'quit' when you are finished.")

while True:
    topping = input("Enter a pizza topping: ")
    if topping == 'quit':
        break
    else:
        print(f"We will add {topping} to the pizza.")

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

while True:
    print("This is an infinite loop!")

7.3

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

sandwich_orders = ['tuna', 'ham and cheese', 'turkey', 'pastrami', 'pastrami', 'pastrami']
finished_sandwiches = []

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

print("\nList of finished sandwiches:")
for sandwich in finished_sandwiches:
    print(sandwich)

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

sandwich_orders = ['tuna', 'ham and cheese', 'pastrami', 'turkey', 'pastrami', 'pastrami']
finished_sandwiches = []

print("Sorry, we're out of pastrami.")

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

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

print("\nList of finished sandwiches:")
for sandwich in finished_sandwiches:
    print(sandwich)

练习7-10:梦想的度假胜地
编写一个程序,调查用户梦想的度假胜地。使用类似于下面的提示,并编写一个打印调查结果的代码块。

dream_vacation = input("If you could visit one place in the world, where would you go? ")
print(f"Your dream vacation spot is {dream_vacation}.")

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值