高级编程技术作业_8

7-4 比萨配料

  题目描述:编写一个循环,提示用户输入一系列的比萨配料,并在用户输入'quit'时结束循环。每当
用户输入一种配料后,都打印一条消息,说我们会在比萨中添加这种配料。

INPUT
potato
tomato
fish
quit
OUTPUT
Please input an ingredient
We will put potato in pizza
Please input an ingredient
We will put tomato in pizza
Please input an ingredient
We will put fish in pizza
代码展示:

while message != "quit":
    message = input("Please input an ingredient\n")
    if message != 'quit':
        print("We will put " + message + " in pizza")

7-5 电影票

  题目描述:有家电影院根据观众的年龄收取不同的票价:不到三岁的观众免费;三到十二岁的观众为
  10美元;超过12岁的观众为15美元。请编写一个循环,在其中询问用户的年龄,并指出其票价。

INPUT
2
5
15
OUTPUT
How old are you?
2
You don’t need to pay
How old are you?
5
You need to pay 10 dollars
How old are you?
15
You need to pay 15 dollars

代码展示:

age = ''
while 1 :
  age = input("How old are you?\n")
  if age == 'quit':
    break

  num = int(age)
  if num < 3 and num > 0:
    print("You don't need to pay")
  elif num >=3 and num <= 12:
    print("You need to pay 10 dollars")
  elif num > 12:
    print("You need to pay 15 dollars")

7-6 三个出口

  题目描述:以另一种方式完成练习7-4或7-5,在程序中爱去如下所有做法。
  ·在while循环中使用条件测试来结束循环。
  ·使用变量active来控制循环结束的时机。
  ·使用break语句在用户输入‘quit’时退出循环。

INPUT
2
5
15
OUTPUT
How old are you?
2
You don’t need to pay
How old are you?
5
You need to pay 10 dollars
How old are you?
15
You need to pay 15 dollars

代码展示:

#ver1
age = ''
while age != 'quit' :
  age = input("How old are you?\n")

  num = int(age)
  if num < 3 and num > 0:
    print("You don't need to pay")
  elif num >=3 and num <= 12:
    print("You need to pay 10 dollars")
  elif num > 12:
    print("You need to pay 15 dollars")

#ver2
active = 0
while active != 1:
  age = input("How old are you?\n")
  if age == 'quit':
    active = 1

  else:
    num = int(age)
    if num < 3 and num > 0:
      print("You don't need to pay")
    elif num >=3 and num <= 12:
      print("You need to pay 10 dollars")
    elif num > 12:
      print("You need to pay 15 dollars")

#ver3
while 1 :
  age = input("How old are you?\n")
  if age == 'quit':
    break

  num = int(age)
  if num < 3 and num > 0:
    print("You don't need to pay")
  elif num >=3 and num <= 12:
    print("You need to pay 10 dollars")
  elif num > 12:
    print("You need to pay 15 dollars")

7-8 熟食店

  题目描述:创建一个名为sandwich_orders的列表,在其中包含各种三明治的名字;再创建一个名为
  finished_sandwiches的空列表。遍历列表sandwich_orders,对于其中的美中三明治,都打印一
  条消息,如I made your tuna sandwich,并将其移到列表finished_sandwiches。所有三明治
  都制作好后,打印一条消息,将这些三明治列出来。

INPUT

OUTPUT

代码展示:

sandwich_orders = ['tuna sandwich', 'cheese sandwich', 'mayo sandwich']
finished_sandwiches = []

while sandwich_orders:
   sandwich = sandwich_orders.pop()   
   print('I made your ' + sandwich)
   finished_sandwiches.append(sandwich)


for sandwich in finished_sandwiches:
   print(sandwich)

猜你喜欢

转载自blog.csdn.net/akago9/article/details/79701182