第七章主要是继续介绍Python中的输入操作和while
,是比较简单的一章
7-1 汽车租赁
编写一个程序,询问用户要租赁什么样的汽车,并打印一条消息,如
“Let me see if I can find you a Subaru”
。
car = input("What kind of car do you want? ")
print("Let me see if I can find you a " + car)
#输出 What kind of car do you want?
#输入 "Benz"
#输出 Let me see if I can find you a Benz
7-2 餐馆订位
编写一个程序,询问用户有多少人用餐。如果超过
8
人,就打印一条消息,指出没有空桌;否则指出有空桌。
client = int(input("How many people of you? "))
if client > 8:
print("Sorry, there's no table.")
else:
print("Welcome! Please follow me!")
#输出 How many people of you?
#输入 9
#输出 Sorry, there's no table.
7-3 10
的整数倍
让用户输入一个数字,并指出这个数字是否是
10
的整数倍。
num = int(input("Enter a numbers:"))
if num % 10 == 0:
print("the nums is times of 10")
else:
print("the nums is not times of 10")
#输出 Enter a numbers:
#输入 20
#输出 the nums is times of 10
7-4 比萨配料
编写一个循环,提示用户输入一系列的比萨配料,并在用户输入
'quit'
时结束循环。每当用户输入一种配料后,都打印一条消息,说我们会在比萨中添加这种配料。
while True:
thing = input("Please Enter some things you want to add, enter Q to quit:")
if thing == 'Q':
break
print("Ok, we will add " + thing)
#输出 Please Enter some things you want to add, enter Q to quit:
#输入 "apple"
#输出 Ok, we will add apple
#输出 Please Enter some things you want to add, enter Q to quit:
#输入 "milk"
#输出 Ok, we will add milk
#输出 Please Enter some things you want to add, enter Q to quit:
#输入 "Q"
7-5 电影票
有家电影院根据观众的年龄收取不同的票价:不到
3
岁的观众免费;3~12
岁的观众为10
美元;超过12
岁的观众为15
美元。请编写一个循环,在其中询问用户的年龄,并指出其票价。
age = int(input("How old are you?"))
if age < 3:
print("The price is free")
elif age >= 3 and age < 12:
print("The price is $10")
else:
print("The price is $15")
#输出 How old are you?
#输入 19
#输出 The price is $15
注意if语句的写法:
- 不能用
&&
而用and
- 不能有
else if
而应该用elif
7-6 三个出口
以另一种方式完成练习
7-4
或练习7-5
,在程序中采取如下所有做法。
- 在
while
循环中使用条件测试来结束循环。- 使用变量
active
来控制循环结束的时机。- 使用
break
语句在用户输入'quit'
时退出循环。
选择练习7-5
:
active = 3
while active > 0:
active = active - 1
age = int(input("How old are you? "))
if age < 3:
print("The price is free")
elif age >= 3 and age < 12:
print("The price is $10")
else:
print("The price is $15")
#输出 How old are you?
#输入 19
#输出 The price is $15
#输出 How old are you?
#输入 9
#输出 The price is $10
#输出 How old are you?
#输入 2
#输出 The price is free
while True:
age = int(input("How old are you? Enter 0 to quit: "))
if age == 0:
break;
elif age < 3:
print("The price is free")
elif age >= 3 and age < 12:
print("The price is $10")
else:
print("The price is $15")
#输出 How old are you? Enter 0 to quit:
#输入 6
#输出 The price is $10
#输出 How old are you? Enter 0 to quit:
#输入 2
#输出 The price is free
#输出 How old are you? Enter 0 to quit:
#输入 15
#输出 The price is $15
#输出 How old are you? Enter 0 to quit:
#输入 0
7-7 无限循环
编写一个没完没了的循环,并运行它(要结束该循环,可按
Ctrl +C
,也可关闭显示输出的窗口)。
while True:
print("in while")
#输出:
#in while
#in while
#in while
#in while
#...
7-8 熟食店
创建一个名为
sandwich_orders
的列表,在其中包含各种三明治的名字;再创建一个名为finished_sandwiches
的空列表。遍历列表sandwich_orders
,对于其中的每种三明治,都打印一条消息,如I made your tuna sandwich
,并将其移到列表finished_sandwiches
。所有三明治都制作好后,打印一条消息,将这些三明治列出来。
sandwich_orders = ["A", "B", "C"]
finished_sandwiches = []
while sandwich_orders:
sandwich = sandwich_orders.pop()
print("I made " + sandwich + " sandwich")
finished_sandwiches.insert(0, sandwich)
print("All sandwiches are made")
#输出:
#I made C sandwich
#I made B sandwich
#I made A sandwich
#All sandwiches are made
7-9 五香烟熏牛肉(pastrami)
卖完了
使用为完成练习
7-8
而创建的列表sandwich_orders
,并确保'pastrami'
在其中至少出现了三次。在程序开头附近添加这样的代码:打印一条消息,指出熟食店的五香烟熏牛肉卖完了;再使用一个while
循环将列表sandwich_orders
中的'pastrami'
都删除。确认最终的列 表finished_sandwiches
中不包含'pastrami'
。
sandwich_orders = ["A", 'pastrami', "B", 'pastrami', "C", 'pastrami']
print("The pastrami sandwich has been sold out.")
while 'pastrami' in sandwich_orders:
sandwich_orders.remove('pastrami')
print(sandwich_orders)
#输出:
#The pastrami sandwich has been sold out.
#['A', 'B', 'C']
7-10 梦想的度假胜地
编写一个程序,调查用户梦想的度假胜地。使用类似于
“If you could visit one place in the world, where would you go?”
的提示,并编写一个打印调查结果的代码块。
surveys = {}
print("Survey begin:")
while True:
ask = input("If you could visit one place in the world, where would you go? Q to quit\n")
if ask == 'Q':
break
elif ask in surveys.keys():
surveys[ask] += 1
else:
surveys[ask] = 1
for key, value in surveys.items():
print(str(value) + " people like to go to " + key)
#输出 Survey begin:
#输出 If you could visit one place in the world, where would you go? Q to quit
#输入 "a"
#输出 If you could visit one place in the world, where would you go? Q to quit
#输入 "a"
#输出 If you could visit one place in the world, where would you go? Q to quit
#输入 "b"
#输出 If you could visit one place in the world, where would you go? Q to quit
#输入 "c"
#输出 If you could visit one place in the world, where would you go? Q to quit
#输入 "a"
#输出 If you could visit one place in the world, where would you go? Q to quit
#输入 "Q"
#输出 3 people like to go to a
#输出 1 people like to go to c
#输出 1 people like to go to b