Python编程:从入门到实践(课后习题:第7章 用户输入和while循环)

coding:utf-8!

7-1汽车租赁

message = input("Type what kind of car do you want to rent? ")
print("Let me see if I can find you a " + message.title() + “.”)

print()

7-2餐馆订位

number = input("How many people will come to have lunch? ")
number = int(number)
if number >= 8:
print(“Sorry, we don’t have empty table here for so many people.”)
else:
print(“Sure, we have empty tables here.”)

print()

7-3数字10的整数倍

num = input(“Type a number, and I’ll tell you if it is a multiple of ten”)
num = int(num)
if num % 10 == 0:
print("Yes, number " + str(num) + " is a multiple of ten. ")
else:
print(“No, number " + str(num) + " is not a multiple of ten.”)

7-4比萨配料

prompt = “\nPlease enter the ingredients you want to add to your pizza.”
prompt += “Type ‘quit’ to indicate that you need the above ingredients.”
active = True
while active:
ingredients = input(prompt) # 注意这里的赋值是用input(prompt)
if ingredients == ‘quit’:
active = False
else:
print(ingredients.title() + " has been added to the pizza.")

print()

7-5电影票

age = input(“Enter your age, and I will tell you your ticket price.”)
age = int(age)
if age < 3:
print(“You are free to visit.”)
elif 3 <= age < 12:
print(“Your ticket price is 10 dollars.”)
else:
print(“Your ticket price is 15 dollars.”)

结合网上参考答案改编

prompt = “\nPlease enter your age.”
prompt += “Enter ‘quit’ to exit the loop.”
while True:
age = input(prompt)
if age == ‘quit’:
break
else:
age = int(age) # int()将数字的字串符转换为数值表示
if age < 3:
print(“For free!”)
elif age >= 3 and age < 12:
print(“10 dollars!”)
elif age >= 12:
print(“15 dollars!”)

7-6三个出口

print()
active = True
while active:
str = input("Please input a ingredient: ")
if str == ‘quit’:
active = False
else:
print(“I’ll add " + str + " on pizza.”)
print()
while True:
str = input("Please input a ingredient: ")
if str == ‘quit’:
break
else:
print(“I’ll add " + str + " on pizza.”)

7-7无限循环

x = 1
while x < 5:
print(x)
x += 1 # 去掉这句即无限循环,按ctrl+c结束循环

7-8熟食店

print()
sandwich_orders = [‘tuna’, ‘smoked meat’, ‘katsu sando’,]
finished_sandwiches = []
while sandwich_orders:
sandwich = sandwich_orders.pop()
print(“I made your " + sandwich + " sandwich.”)
finished_sandwiches.append(sandwich)
print(“All the sandwiches are done, including: “)
for sandwich in finished_sandwiches:
print(”\t” + sandwich)

7-9五香烟熏牛肉(pastrami)卖完了

print()
sandwich_orders = [‘pastrami’, ‘tuna’, ‘smoked meat’, ‘pastrami’, ‘katsu sando’, ‘pastrami’]
print(“Pastrami has been sold out.”)
for pastrami in sandwich_orders:
sandwich_orders.remove(‘pastrami’)
print(sandwich_orders)

7-10梦想的度假胜地

places = {}
polling_active = True
while polling_active:
name = input("\nWhat’s your name? “)
place = input(“If you could visit one place in the world, where would you go?”)
places[name] = place
repeat = input(“Would you let another person to take this poll?(yes/no)”)
if repeat == ‘no’:
polling_active = False
print(”\n— Polling Results —")
for name, place in places.items():
print(name.title() + " would like to go to " + place.title() + “.”)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值