#7-2 num=input("How many people will come to dinner? ") if int(num)>8: print("Sorry, there isn't empty table big enough for you.") else: print("We can arrange an empty table for you.") print('') #7-4 ingredient=input("Please enter the ingredient you want to add into pizza(Enter 'quit' to stop): ") while ingredient!='quit': print("We will add "+ingredient+' into pizza.') ingredient=input("Please enter the ingredient you want to add into pizza: ") print('') #7-5 age=input("How old are you? ") age=int(age) if age<3: print("The ticket is free.") elif age<=12: print("The ticket is $10.") else: print("The ticket is $15.") print('') #7-8 sandwich_orders=["pastrami sandwich","tuna sandwich","pastrami sandwich","beef sandwich","pastrami sandwich","chicken sandwich"] finished_sandwiches=[] print("Sorry, the patrami sandwich has been sold out.") while "pastrami sandwich" in sandwich_orders: sandwich_orders.remove("pastrami sandwich") for sandwich in sandwich_orders: print("I made your "+sandwich+'.') finished_sandwiches.append(sandwich) for sandwich in finished_sandwiches: print("The "+sandwich+" is finished.") print("") #8-2 def favorite_book(title): print("One of my favorite book is "+title+".") favorite_book("Alice in Wonderland") print("") #8-4 def make_shirt(words="I love Python",size='L'): print("We have to make a T-shirt of size "+size+" with '"+words+"'.") make_shirt() make_shirt(size='M') make_shirt('Hello world') print('') #8-8 def make_album(singer,album,song_num=0): if int(song_num)==0: return {'singer':singer,'album':album} else: return {'singer':singer,'album':album,'song_num':song_num} while True: singer=input("Please input the name of the singer(Enter 'quit'to quit): ") if singer=='quit': break album=input("Please input the name of the album(Enter 'quit'to quit): ") if album=='quit': break song_num=input("Please input the number of the songs in the album(Enter '0' if you don't know): ") if singer=='0': print(make_album(singer,album)) else: print(make_album(singer,album,song_num)) print('') #8-11 def show_magicians(magicians): for magician in magicians: print(magician) def make_great(magicians): great_magicians=[] for magician in magicians: great_magicians.append('the Great '+magician) return great_magicians magicians=['Tony','Max','Neil'] great_magicians=make_great(magicians[:]) show_magicians(magicians) show_magicians(great_magicians) print('') #8-14 def make_car(manufactor,model,**info): car={} car['manufactor']=manufactor car['model']=model for key,value in info.items(): car[key]=value return car car=make_car('subaru','outback',color='blue',tow_package='True') print(car)
上述代码输入输出如下:
How many people will come to dinner? 12 Sorry, there isn't empty table big enough for you. Please enter the ingredient you want to add into pizza(Enter 'quit' to stop): cheese We will add cheese into pizza. Please enter the ingredient you want to add into pizza: beef We will add beef into pizza. Please enter the ingredient you want to add into pizza: quit How old are you? 18 The ticket is $15. Sorry, the patrami sandwich has been sold out. I made your tuna sandwich. I made your beef sandwich. I made your chicken sandwich. The tuna sandwich is finished. The beef sandwich is finished. The chicken sandwich is finished. One of my favorite book is Alice in Wonderland. We have to make a T-shirt of size L with 'I love Python'. We have to make a T-shirt of size M with 'I love Python'. We have to make a T-shirt of size L with 'Hello world'. Please input the name of the singer(Enter 'quit'to quit): Adele Please input the name of the album(Enter 'quit'to quit): 25 Please input the number of the songs in the album(Enter '0' if you don't know): 0 {'singer': 'Adele', 'album': '25'} Please input the name of the singer(Enter 'quit'to quit): Maroon 5 Please input the name of the album(Enter 'quit'to quit): V Please input the number of the songs in the album(Enter '0' if you don't know): 10 {'singer': 'Maroon 5', 'album': 'V', 'song_num': '10'} Please input the name of the singer(Enter 'quit'to quit): quit Tony Max Neil the Great Tony the Great Max the Great Neil {'manufactor': 'subaru', 'model': 'outback', 'color': 'blue', 'tow_package': 'True'}