当前位置:   article > 正文

Python编程 从入门到实践 第八章习题_头歌python第八章答案

头歌python第八章答案
8-2 喜欢的图书:
  1. def favorite_book(title):
  2. """"喜欢的图书"""
  3. print("One of my favorite book is " + title.title() + ".")
  4. favorite_book("Harry Potter")

输出:

One of my favorite book is Harry Potter.


8-4 大号T恤:

  1. def make_shirt(size, word = "I love Python"):
  2. print("\nSize: " + size )
  3. print("Words: " + word)
  4. make_shirt("L")
  5. make_shirt("M")
  6. make_shirt("S",word = "I love C++")

输出:

  1. Size: L
  2. Words: I love Python
  3. Size: M
  4. Words: I love Python
  5. Size: S
  6. Words: I love C++


8-7 专辑:

  1. def make_album(singer_name, ablum_name, numebr = ""):
  2. """返回专辑的信息"""
  3. ablum = {'singer': singer_name, 'ablum': ablum_name}
  4. if numebr:
  5. ablum['number'] = numebr;
  6. return ablum
  7. ablum = make_album("Taylor Swift","Reputation",15)
  8. print(ablum)
  9. ablum2 = make_album("Taylor Swift", "Fearless")
  10. print(ablum2)
输出:
  1. {'singer': 'Taylor Swift', 'ablum': 'Reputation', 'number': 15}
  2. {'singer': 'Taylor Swift', 'ablum': 'Fearless'}


8-8 用户的专辑

  1. def make_album(singer_name, ablum_name, numebr = ""):
  2. """返回专辑的信息"""
  3. ablum = {'singer': singer_name, 'ablum': ablum_name}
  4. if numebr:
  5. ablum['number'] = numebr;
  6. return ablum
  7. while True:
  8. print("\nPlease enter the name of singer: ")
  9. singer_name = input("Singer Name:")
  10. if singer_name == 'q':
  11. break;
  12. print("Please enter the name of ablum: ")
  13. ablum_name = input("Ablum Name: ")
  14. if ablum_name == 'q':
  15. break;
  16. ablum = make_album(singer_name,ablum_name)
  17. print(ablum)
输出:
  1. Please enter the name of singer:
  2. Singer Name:Taylor Swift
  3. Please enter the name of ablum:
  4. Ablum Name: Reputation
  5. {'singer': 'Taylor Swift', 'ablum': 'Reputation'}
  6. Please enter the name of singer:
  7. Singer Name:q


8-9 魔术师:

  1. def show_magicians(names):
  2. for name in names:
  3. msg = "Hello, " + name.title() + "!"
  4. print(msg)
  5. names = ['Alex', 'Leo', 'Mike']
  6. show_magicians(names)
输出:
  1. Hello, Alex!
  2. Hello, Leo!
  3. Hello, Mike!

8-10 了不起的魔术师:

  1. def show_magicians(names):
  2. for name in names:
  3. msg = "Hello, " + name.title() + "!"
  4. print(msg)
  5. def make_great(names,new_names):
  6. while names:
  7. current_name = names.pop()
  8. current_name = "the Great " + current_name
  9. new_names.append(current_name)
  10. names = ['Alex', 'Leo', 'Mike']
  11. new_names = []
  12. print("The Original magician: ")
  13. show_magicians(names)
  14. make_great(names,new_names)
  15. print("\nThe present magicians: ")
  16. show_magicians(new_names)

输出:

  1. The Original magician:
  2. Hello, Alex!
  3. Hello, Leo!
  4. Hello, Mike!
  5. The present magicians:
  6. Hello, The Great Mike!
  7. Hello, The Great Leo!
  8. Hello, The Great Alex!

8-14 汽车:

  1. def make_car(factory, type, **car_info):
  2. car = {}
  3. car['factory'] = factory
  4. car['type'] = type
  5. for key, value in car_info.items():
  6. car[key] = value
  7. return car
  8. car_info = make_car('subaru', 'outback', color = 'blue', tow_packet = True)
  9. print(car_info)

输出:

{'factory': 'subaru', 'type': 'outback', 'color': 'blue', 'tow_packet': True}

8-15 打印模型

  1. #print_funtions.py
  2. def print_fun(name):
  3. print("Hello, " + name)
  1. #print_models.py
  2. import print_funtions as p_fun
  3. p_fun.print_fun("Alex")
输出:
Hello, Alex









声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/616894
推荐阅读
相关标签
  

闽ICP备14008679号