Python编程:从入门到实践——用户输出和while 循环(第七章+课后答案)

第七章 用户输出和while 循环(所有习题答案请自己输入,不要粘贴复制)

习题备忘,以便于后续学习

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

car = input("what kind of car do you need to rent? ")
print(f"\nLet me see if I can find you a {car}")

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

people = input("how many people are dining? ")
people = int(people)
if people > 8:
    print("\n there are no available tables at the moment.")
else:
    print("\nThere is an available table now")

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

number = input("enter a number, and I will tell you if it is a multiple of 10 ")
number = int(number)
if number % 10 == 0:
    print(f" \nthe number {number} is a multiple of 10 ")
else:
    print(f" \nthe number {number} is not a multiple of 10 ")

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

pizza = "\nTell us what do you want for your ingredient"
pizza += "\nEnter 'quit' to end the program. "
ingredient = ''
while ingredient != 'quit':
    ingredient = input(pizza)
    if ingredient != 'quit':
        print(f"\nwe will add {ingredient.title()} to your pizza")

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

prompt = "\nTell us how old are you"
prompt += "\nEnter 'quit' to end the program. "
active = True
while active:
    age = input(prompt)
    if age == 'quit':
        active = False
    else:
        age = int(age)
        if age < 3:
            print("\nFor children under, Free.")
        elif age <= 12:
             print("\n10 dollars for children between 3 and 12.")
        else:
            print("\n15 dollars for people over 12.")

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

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

prompt = "\nTell us how old are you"
prompt += "\nEnter 'quit' to end the program. "
age = ''
while age != 'quit':
    age = input(prompt)
    if age == 'quit':
        print("\nThank you")
    else:
        age = int(age)
        if age <3:
             print("\nFor children under, Free.")
        elif age <= 12:
             print("\n10 dollars for children between 3 and 12.")
        else:
             print("\n15 dollars for people over 12.")

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

prompt = "\nTell us how old are you"
prompt += "\nEnter 'quit' to end the program. "
active = True
while active:
    age = input(prompt)
    if age == 'quit':
        active = False
    else:
        age = int(age)
        if age < 3:
            print("\nFor children under, Free.")
        elif age <= 12:
             print("\n10 dollars for children between 3 and 12.")
        else:
            print("\n15 dollars for people over 12.")

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

prompt = "\nTell us how old are you"
prompt += "\nEnter 'quit' to end the program. "
while True:
    age = input(prompt)
    if age == 'quit':
        break
    else:
        age = int(age)
        if age < 3:
            print("\nFor children under, Free.")
        elif age <= 12:
             print("\n10 dollars for children between 3 and 12.")
        else:
            print("\n15 dollars for people over 12.")

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

x = 1

while x <= 2:

print(x)

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

sandwich_orders = ['pastrami','tuna','cod fish']
finished_sandwiches = []
while sandwich_orders:
    current_order = sandwich_orders.pop()
    print(f"I made your {current_order.title()} sandwich.")
    finished_sandwiches.append(current_order)
print("\nThe following sandwiches have been finished")
for finished_sandwich in finished_sandwiches:
    print(finished_sandwich.title())

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

sandwich_orders = ['pastrami','tuna','pastrami','cod fish','salami','pastrami']
finished_sandwiches = []
print(sandwich_orders)
print(f"\nPastrami is out of order. ")
while 'pastrami' in sandwich_orders:
    sandwich_orders.remove('pastrami')
while sandwich_orders:
    current_order = sandwich_orders.pop()
    print(f"I made your {current_order.title()} sandwich.")
    finished_sandwiches.append(current_order)
print("\nThe following sandwiches have been finished")
for finished_sandwich in finished_sandwiches:
    print(finished_sandwich.title())

练习7-10 梦想的度假胜地 编写一个程序,调查用户梦想的度假胜地。使用类似下面的提示,并编写一个打印调查结果的代码。If  you could visit one place in the world, where would you go?

places = {}
polling_active = True
while polling_active:
    name = input("\nWhat is your name? ")
    place = input("\nIf you could visit one place in the world,where would you go? ")
    places[name] = place
    repeat = input("would you like to let another person respond?(yes/ no) ")
    if repeat == 'no':
        polling_active = False
print("\n---poll results---")
for name,place in places.items():
    print(f"{name} would like to go {place}.")

  • 21
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值