参考书籍–《python编程从入门到实践》
本章总结
|动手试一试|
# 7.1汽车租赁
car = input("let me see if I can find you a Subaru:") #python2.7为raw_input()
print(car)
let me see if I can find you a Subaru:audi
audi
# 7.2 餐馆订位
Order = input("how many people will have dinner:")
order = int(Order) #input()获得的是字符串,int()将字符串转化为数值类型
if order>8:
print("there is no table available.")
else:
print("there are tables available for you.")
how many people will have dinner: 2
there are tables available for you.
# 7.3 10的整数倍
number = input("input a number:")
number = int(number)
if number % 10 == 0: #取模运算,判断整除、奇偶数
print("是10的整数倍")
else:
print("不是10的整数倍")
input a number:2
不是10的整数倍
# 7.4 比萨配料
#1、while循环
ingredient = "\nplease input a series of pizza toppings:"
ingredient += "\nEnter 'quit' to end."
topping = ""
while topping != 'quit':
topping = input(ingredient)
if topping != 'quit':
print("We will add "+ topping.title() + " into pizza!")
# 7.4 比萨配料
#2、使用标记
ingredient = "\nplease input a series of pizza toppings:"
ingredient += "\nEnter 'quit' to end."
active = True
while active:
topping = input(ingredient)
if topping == 'quit':
active = False
else:
print("We will add "+ topping.title() + " into pizza!")
# 7.4 比萨配料
#3、break语句
ingredient = "\nplease input a series of pizza toppings:"
ingredient += "\nEnter 'quit' to end."
while True:
topping = input(ingredient)
if topping == 'quit':
break
else:
print("We will add "+ topping.title() + " into pizza!")
# 7.5 电影票
age = input("\nhow old are you: ")
age = int(age)
if age < 3:
print("the ticket is free.")
elif age > 12:
print("the ticket is 15$.")
else:
print("the ticket is 10$.")
# 7.6 三个出口:用while循环、标记、break语句完成7.4或7.5的练习
# 7.7 无限循环
x=1
while x>5:
print(x)
#7.8 熟食店
sandwich_orders = ['tuna','pastrami','hamburger','tuna','cheese','pastrami','chicken','pastrami']
finished_sandwiches = []
#正序输出
for sandwich_order in sandwich_orders:
print("I made your " +sandwich_order+" sandwich")
finished_sandwiches.append(sandwich_order)
#倒叙输出
#while sandwich_orders:
# current_sandwich = sandwich_orders.pop()
# print("I made your " +current_sandwich+" sandwich")
# finished_sandwiches.append(current_sandwich)
print("\nThe following sandwiches have been finished:")
for finished_sandwich in finished_sandwiches:
print(finished_sandwich)
I made your tuna sandwich
I made your pastrami sandwich
I made your hamburger sandwich
I made your tuna sandwich
I made your cheese sandwich
I made your pastrami sandwich
I made your chicken sandwich
I made your pastrami sandwich
The following sandwiches have been finished:
tuna
pastrami
hamburger
tuna
cheese
pastrami
chicken
pastrami
# 7.9五香烟熏牛肉(pastrami)卖完了,接7.8
sandwich_orders = ['tuna','pastrami','hamburger','tuna','cheese','pastrami','chicken','pastrami']
print("Pastrami sandwich has been sold out.\n")
while 'pastrami' in sandwich_orders:
sandwich_orders.remove('pastrami')
print(sandwich_orders)
Pastrami sandwich has been sold out.
['tuna', 'hamburger', 'tuna', 'cheese', 'chicken']
# 7.10 梦想的度假胜地
responses = {}
polling_active = True
while polling_active:
name = input("\nwhat's your name? ")
place = input("\nif you could visit one place in the world, where would you go? ")
responses[name] = place
repeat = input("would you like to let another person join the investigation? (yes/no) ")
if repeat == 'no':
polling_active = False
print("\n--- Poll Results ---")
for name, place in responses.items():
print(name + " would like to visit " + place + ".")
what's your name? LYL
if you could visit one place in the world, where would you go? Florence
would you like to let another person join the investigation? (yes/no) yes
what's your name? QQ
if you could visit one place in the world, where would you go? American
would you like to let another person join the investigation? (yes/no) no
--- Poll Results ---
LYL would like to visit Florence.
QQ would like to visit American.