知识清单
动手试一试
- 汽车租赁:编写一个程序询问用户要租赁什么样的汽车并打印一条消息
car = input('Plesea input the tpye of car you want:')
print('You choose the type of car is '+ car)
结果:
Plesea input the tpye of car you want:BWM
You choose the type of car is BWM
- 餐厅订位:询问用户有多少人用餐,如果超过8人就打印消息没有空位,否则有
num = input('Please input how many people : ')
num = int(num)
if num >8:
print('Sorry!There is not a table for you now.')
else:
print('OK!Please wait a moument.')
结果:
Please input how many people : 7
OK!Please wait a moument.
Please input how many people : 9
Sorry!There is not a table for you now.
- 10的整倍数:让用户输入一个数字指出该数是否是10的整倍数
num = input('Please input a number: ')
num = int(num)
if num%10 == 0:
print('The number is multiple of ten.')
else:
print('The number is not multiple of ten.')
结果:
Please input a number: 24
The number is not multiple of ten.
Please input a number: 100
The number is multiple of ten.
- 披萨原料:编写一个循环,提示用户输入一系列的比萨配料,并在用户输入’quit’时结束循环。每当用户输入一种配料后,都打印一条消息,说我们会在比萨中添加这种配料。
ingredients = '\nPlease choose waht you want to eat:'
ingredients += "\nEnter 'quit' to end."
flag = True
while flag:
messeage = input(ingredients)
#print('OK.We will add it in pizza.')
if messeage =='quit':
flag = False
else:
print('OK.We will add '+messeage+'it in pizza.\n')
结果:
Please choose waht you want to eat:
Enter 'quit' to end.tomato
OK.We will add tomatoit in pizza.
Please choose waht you want to eat:
Enter 'quit' to end.beef
OK.We will add beefit in pizza.
Please choose waht you want to eat:
Enter 'quit' to end.quit
Process finished with exit code 0
- 电影票:有家电影院根据观众的年龄收取不同的票价:不到 3 岁的观众免费;3~12 岁的观众为 10 美元;超过 12 岁的观众为 15 美元。请编写一个循环,在其中询问用户的年龄,并指出其票价。
age = input("How old are you? ")
age = int(age)
if age < 3:
print("Free")
elif 3 < age < 12:
print("10 dollars.")
else:
print("15 dollars.")
- 三个出口:以另一种方式完成练习 7-4 或练习 7-5,在程序中采取如下所有做法。
1) 在 while 循环中使用条件测试来结束循环。
2) 使用变量 active 来控制循环结束的时机。
3)使用 break 语句在用户输入’quit’时退出循环。
active = True
while active:
age = input("How old are you? ")
if age == "quit":
active = False
break
age = int(age)
if age < 3:
print("Free fare.")
elif 3 < age < 12:
print("The fare is 10 dollars.")
else:
print("The fare is 15 dollars.")
结果:
How old are you? 21
The fare is 15 dollars.
How old are you? 12
The fare is 15 dollars.
How old are you? quit
Process finished with exit code 0
- 无限循环: 编写一个没完没了的循环,并运行它(要结束该循环,可按 Ctrl +C,也可关闭显示输出的窗口)。
a = 1
while a <10:
print(a)
- 熟食店: 创建一个名为 sandwich_orders 的列表,在其中包含各种三明治的名字;再创建一个名为 finished_sandwiches 的空列表。遍历列表 sandwich_orders,对于其中的每种三明治,都打印一条消息,如 I made your tuna sandwich,并将其移到列finished_sandwiches。所有三明治都制作好后,打印一条消息,将这些三明治列出来。
sandwich_orders = ['BBQ sandwich','fruit sandwich','vegetable sandwich']
finished_sandwich = []
while sandwich_orders:
sandwich = sandwich_orders.pop()
print('I made your '+ sandwich)
finished_sandwich.append(sandwich)
print(finished_sandwich)
结果:
I made your vegetable sandwich
I made your fruit sandwich
I made your BBQ sandwich
['vegetable sandwich', 'fruit sandwich', 'BBQ sandwich']
- 五香熏牛肉(pastrami卖完了)使用为完成练习 7-8 而创建的列表sandwich_orders,并确保’pastrami’在其中至少出现了三次。在程序开头附近添加这样的代码:打印一条消息,指出熟食店的五香烟熏牛肉卖完了;再使用一个 while 循环将列表 sandwich_orders 中的’pastrami’都删除。确认最终的列表 finished_sandwiches 中不包含’pastrami’。
print('Pastrami is null.')
sandwich_orders = ['BBQ sandwich','fruit sandwich','vegetable sandwich']
sandwich_orders.append('pastrami')
sandwich_orders.append('pastrami')
sandwich_orders.append('pastrami')
# print(sandwich_orders) ['BBQ sandwich', 'fruit sandwich', 'vegetable sandwich', 'pastrami', 'pastrami', 'pastrami']
finished_sandwich = []
while 'pastrami' in sandwich_orders:
sandwich_orders.remove('pastrami')
while sandwich_orders:
sandwich = sandwich_orders.pop()
print('\nI made your '+ sandwich)
finished_sandwich.append(sandwich)
print('finished_sandwich:'+str(finished_sandwich))
结果:
Pastrami is null.
I made your vegetable sandwich
finished_sandwich:['vegetable sandwich']
I made your fruit sandwich
finished_sandwich:['vegetable sandwich', 'fruit sandwich']
I made your BBQ sandwich
finished_sandwich:['vegetable sandwich', 'fruit sandwich', 'BBQ sandwich']
- 梦想的度假胜地:编写一个程序,调查用户梦想的度假胜地。使用类似于“If you could visit one place in the world, where would you go?”的提示,并编写一个打印调查结果的代码块。
resorts = {}
flag = True
while flag:
name = input('\nYour name is: ')
resort = input('If you could visit one place in the world, where would you go? ')
resorts[name] = name
resorts[resort]=resort
repeat = input("Would you like to let another person respond? (y/ n)")
if repeat == 'n':
flag = False
for name,resort in resorts.items():
print(name+' likes resorts include '+resort)
结果:
Your name is: TuTu
If you could visit one place in the world, where would you go? Pairs
Would you like to let another person respond? (y/ n)y
Your name is: CK
If you could visit one place in the world, where would you go? Zunyi
Would you like to let another person respond? (y/ n)n
TuTu likes resorts include TuTu
Pairs likes resorts include Pairs
CK likes resorts include CK
Zunyi likes resorts include Zunyi
小结:
使用input()来让用户提供信息,处理文本和数字输入;用while循环让程序按用户的要求不断地运行;多种控制while循环流程的方式;设置活动标志;break语句以及continue语句;使用while循环在列表之间移动元素以及从列表中删除包含特定值的元素;结合while和字典。