Python编程:从入门到实践 (第七章习题)

#7-1
#汽车租赁:编写一个程序,询问用户要租赁什么样的汽车,并打印一条消息,
#如“Let me see if I can find you a Subaru”。
car = input("What the kind of car do you want to rent?")
print("Let me see if I can find you a " + car)

#7-2
#餐馆订位:编写一个程序,询问用户有多少人用餐。如果超过8 人,就打印一条消息,指出没有空桌;否则指出有空桌。
number = input("How many people?")
number = int(number)
if number > 8:
   print("Sorry, the desks are not available.")
else:
   print("The desks are available.")

#7-3
#10 的整数倍:让用户输入一个数字,并指出这个数字是否是10 的整数倍。
number = input("Give me a number and I can tell you if it is an integer multiple of 10. ")
number = int(number)
if number % 10 == 0:
   print("It is an integer multiple of 10.")
else:
   print("It isn't an integer multiple of 10.")

#7-4 比萨配料
prompt = "\nWhat kind of toppings do you want?"  # prompt是变量,存的是提示信息,如果提示信息太长,可以写成2行。
prompt += "\nEnter 'quit' to end the program."  # prompt += 的意思是在上行提示信息后,添加等号后的提示信息

active = True
while active:
   message = input(prompt)

   if message == 'quit':
      break
   else:
      print("We will add: " + message + '.')

#7-5 电影票:有家电影院根据观众的年龄收取不同的票价:
# 不到3岁的观众免费;3~12岁的观众为10美元;超过12岁的观众为15美元. 请编写一个循环,在其中询问用户的年龄,并指出其票价.
while True: #while True 打头的循环将不断运行,直到遇到break语句。
   prompt = "\n Please enter your age, and then we will tell you how much you shall pay."
   prompt += "\n Enter 'quit' to end the program."
   age = input(prompt)

   if age == 'quit':
      break
   if int(age) < 3:
      print("Your cost is $0.")
   elif int(age) <= 12:
      print("Your cost is $10.")
   else:
      print("Your cost is $15.")

#7-6 三个出口:以另一种方式完成练习 7-4 或练习 7-5,在程序中采取如下所有做法。(这道题很好,其实就是讲了三种停止循环的办法)
#在 while 循环中使用条件测试来结束循环。(不太理解这种方法)
#使用变量 active 来控制循环结束的时机。
# 使用 break 语句在用户输入'quit'时退出循环。
#1
prompt = "\nWhat kind of toppings do you want?"
prompt += "\nEnter 'quit' to end the program."

message = ""
while message != 'quit': #循环输入值不是quit的情况,执行下面的代码块
   message = input(prompt) #显示上面的提示信息,让用户输入值

   if message != 'quit': #如果输入的值不是quit,就显示下面的代码块的消息
      print("We will add: " + message + '.')

#2
prompt = "\nWhat kind of toppings do you want?"
prompt += "\nEnter 'quit' to end the program."

active = True
while active:
   message = input(prompt)
   if message == 'quit':
      active = False
   else:
      print("We will add: " + message + ".")

#3
prompt = "\nWhat kind of toppings do you want?"  # prompt是变量,存的是提示信息,如果提示信息太长,可以写成2行。
prompt += "\nEnter 'quit' to end the program."  # prompt += 的意思是在上行提示信息后,添加等号后的提示信息

while True:
   message = input(prompt)

   if message == 'quit':
      break
   else:
      print("We will add: " + message + '.')

#7-7 无限循环 :编写一个没完没了的循环,并运行它(要结束该循环,可按Ctrl +C,也可关闭显示输出的窗口)。
#哈哈,这题我一不小心就写对了。

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

sandwich_orders = ['tuna sandwich', 'bacon sandwich', 'egg sandwich']
finished_sandwiches = []
for sandwich_order in sandwich_orders:
   print("I made your " + sandwich_order + ".")

while sandwich_orders:
   making_sandwich = sandwich_orders.pop()
   finished_sandwiches.append(making_sandwich)

print("I have made following sandwiches: ")
for finished_sandwich in finished_sandwiches:
   print(finished_sandwich)

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

print("Pastrami is out of the deli.")

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

print("Only following sandwiches are available: ")
for sandwich_order in sandwich_orders:
   print(sandwich_order.title())

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

responses = {}

poll = True
while poll:
   name = input("What's you name?")
   place = input("If you could visit one place in the world, where would you go?")
   responses[name] = place
   repeat = input("Would you like to let another person respond? (yes/no)")
   if repeat == "no":
      poll = False
print("---Polling Result---")
for name, place in responses.items():
   print("\nIf " + name.title() + " could visit one place in the world, he/she would go " + place.title() + ".") 

















猜你喜欢

转载自blog.csdn.net/treyaa/article/details/80324124