7-1 汽车租赁:
kind = input("What kind of car do you want to rent?")
print("Let me see if I can find you a " + kind + ".")
运行结果:
7-2 餐桌订位:
people = input("How many people have lunch?")
people = int(people)
if people > 8:
print("Sorry.There is no empty table")
else:
print("There are some empty table.")
运行结果:
7-3 10的整数倍:
num = input("输入一个整数: ")
num = int(num);
if num % 10 == 0:
print("这个数字是10的倍数")
else:
print("这个数字不是10的倍数")
运行结果:
7-4 披萨配料:
requested = ""
while True:
requested = input("Enter ingredient:(Enter ‘quit',then quit) ")
if requested == "quit":
break
print("We will add " + requested + " in pizza.")
运行结果:
7-5 电影票:
while True:
age = input("How old are you?")
age = int(age)
if age < 3:
print("You are free.")
elif age <= 12:
print("You should pay $10.")
else:
print("You should pay $15.")
运行结果:
7-8 熟食店:
sandwich_oreders = ["tuna", "hot-dog", "egg"]
finished_sandwiches = []
while sandwich_oreders:
temp = sandwich_oreders.pop(0);
print("I made your " + temp +" sandwich.")
finished_sandwiches.insert(0, temp)
运行结果:
7-9 五项烟熏牛肉卖完了:
sandwich_oreders = ["pastrami", "tuna", "pastrami", "hot-dog", "egg","pastrami"]
print("pastrami was sold out.")
finished_sandwiches = []
while sandwich_oreders:
temp = sandwich_oreders.pop(0)
if temp == "pastrami":
continue
print("I made your " + temp +" sandwich.")
finished_sandwiches.insert(0, temp)
运行结果:
7-10 梦想的度假胜地:
places = []
while True:
place = input("If you could visit one place in the world,where would you go?(Ente 'quit',then quit)")
if place == "quit":
break
places.append(place)
print("The results: ",places)
运行结果:
8-1 消息:
def display_message():
print("We will learn function in this chapter.")
display_message()
运行结果:
8-2 喜欢的图书:
def favorite_book(book):
print("One of my favorite books is " + book + ".")
favorite_book("Alice in Wonderland")
运行结果:
8-3 T恤:
def make_skirt(size, word):
print("The size of skirt is " + size + " and The word of skirt is " + word)
make_skirt("s", "WE")
make_skirt(word = "WE", size = "s")
运行结果:
8-4 大号T恤:
def make_skirt(size="XL", word="I love Python"):
print("The size of skirt is " + size + " and The word of skirt is " + word)
make_skirt()
make_skirt("m")
make_skirt("m", "WE")
运行结果:
8-5 城市:
def describe_city(city, country="China"):
print( city + " is in " + country)
describe_city("Beijing")
describe_city("Najing")
describe_city("New York", "US")
运行结果:
8-6 城市名:
def city_country(city, country):
return city + ", " + country
print(city_country("Beijing", "China"))
print(city_country("Najin", "China"))
print(city_country("New York", "US"))
运行结果:
8-7 专辑:
def make_album(singer_name, album_name, num=""):
album = {"sing_name": singer_name, "album_name":album_name}
if num:
album["num"] = num
return album
print(make_album("Angela", "Over The Rainbow", "10"))
print(make_album("Krist", "six"))
print(make_album("Justin bieber", "My world"))
运行结果:
8-8 用户的专辑:
def make_album(singer_name, album_name, num=""):
album = {"sing_name": singer_name, "album_name":album_name}
if num:
album["num"] = num
return album
while True:
singer_name = input("Singer name:(Enter 'quit', then quit) ")
if singer_name == "quit":break
album_name = input("album name:(Enter 'quit', then quit) ")
if album_name == "quit":break
print(make_album(singer_name, album_name))
运行结果:
8-9 魔术师:
def show_magicians(magicians):
for magician in magicians:
print(magician)
magicians = ['刘谦', '大卫·科波菲尔', '大卫·布莱恩']
show_magicians(magicians)
运行结果:
8-10 了不起的魔术师:
def make_great(magicians):
for num in range(len(magicians)):
magicians[num]="伟大的" + magicians[num]
def show_magicians(magicians):
for magician in magicians:
print(magician)
magicians = ['刘谦', '大卫·科波菲尔', '大卫·布莱恩']
make_great(magicians)
show_magicians(magicians)
运行结果:
8-11 不变的魔术师:
def make_great(magicians):
for num in range(len(magicians)):
magicians[num]="伟大的" + magicians[num]
return magicians
def show_magicians(magicians):
for magician in magicians:
print(magician)
magicians = ['刘谦', '大卫·科波菲尔', '大卫·布莱恩']
others = make_great(magicians.copy())
show_magicians(magicians)
show_magicians(others)
运行结果:
8-12 三明治:
def add_material(*toppings):
print("We will add ")
for topping in toppings:
print(topping)
print("\n")
add_material("hot_dog")
add_material("hot_dog", "beaf")
add_material("hot_dog", "beaf", "egg")
运行结果:
8-13 用户简介:
def build_profile(first, last, **user_info):
profile = {}
profile["first_name"] = first
profile["last_name"] = last
for key, value in user_info.items():
profile[key] = value
return profile
profile=build_profile("chen", "peipeng",
hobby = "running",
favorite_food = "apple",
lover = "cyy"
)
print(profile)
运行结果:
8-14 汽车:
def make_car(producer, size, **car_info):
car = {}
car['producer'] = producer
car['size'] = size
for value, key in car_info.items():
car[value] = key
return car
car_info = make_car('subaru', 'outback', color='bule', tow_package='True')
print(car_info)
运行结果: