Python 从入门到实践 7-4 课后习题

来源:互联网 发布:linux编写shell程序 编辑:程序博客网 时间:2024/06/07 09:36

7.4

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

bullentin = "Please input the bulletin of the pizza! Thank you!"bullentin += "\n(Enter 'quit' when you are finished.)"while True:    name = input(bullentin)    if name =='quit':       break    else:        print("The " + str(name) + ' have been added! ')
7.5

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

先放一段时间。今天有点忙着搞matlab!

while True:    print("Enter 'quit' when you are finished.")    age = input("请输入你的年龄:")    if age == 'quit':        break    elif int(age) < 3:        print("The price of you is free.")    elif 3 <= int(age) <12:        print("The price of you is $10.")    elif 12 <= int(age) :        print("The price of you is $15.")
7.6

三个出口:以另一种方式完成练习7-4 或练习7-5,在程序中采取如下所有做法。
 在while 循环中使用条件测试来结束循环。
 使用变量active 来控制循环结束的时机。
 使用break 语句在用户输入'quit'时退出循环。

flat = Truewhile flat:    print("Enter 'quit' when you are finished.")    age = input("Please input your age:")    if age == 'quit':        flat = False    elif int(age) < 3:        print("The price of you is free.")    elif 3 <= int(age) <12:        print("The price of you is $10.")    elif 12 <= int(age) :        print("The price of you is $15.")