8-1:
def display_message(): print("I am learning function!") display_message()
8-2:
def favorite_book(book): print("My favorite book is "+book.title()) my_book=input("What's your favorite book?\n") favorite_book(my_book)
8-5:
def describe_city(city,country='China'): print(city+" is in "+country+".") describe_city("Shanghai") describe_city("London","British") describe_city(city="New York",country="USA")
8-7:
def make_album(singer,name,number=0): if number: message=singer.title()+" has an album called "+name+" with "+str(number)+" songs!" else: message=singer.title()+" has an album called "+name+"." return message album1=make_album('Jay','Fantasy') print(album1) album2=make_album('Taylor','1984',13) print(album2) album3=make_album('Nogizaka46','君の名は希望') print(album3)
8-10:
def show_magicians(magicians): for magician in magicians: print(magician) def make_great(magicians,great): for magician in magicians: great.append('the Great '+magician) magicians=['Mike','Tom','John'] great=[] show_magicians(magicians) make_great(magicians,great) show_magicians(great)
8-14:
def make_car(producer,style,**information): profile={} profile['producer']=producer profile['style']=style for key,value in information.items(): profile[key]=value return profile car=make_car('sabaru','outback',color='blue',tow_pack=True) print(car)
8-15:
printing_functions.py
def print_models(unprinted_designs,completed_models): while unprinted_designs: current_design=unprinted_designs.pop() print("Printing model: "+current_design) completed_models.append(current_design) def show_completed_models(completed_models): print("\nThe following models have been printed:") for completed_model in completed_models: print(completed_model)
import printing_functions unprinted_designs=['Iphone','Xiaomi','Samsung'] completed_models=[] printing_functions.print_models(unprinted_designs,completed_models) printing_functions.show_completed_models(completed_models)