python從入門到實踐chapter08

def greet_user(username):
    """Display a simple greeting."""
    print("Hello, " + username.title() + "!")    
greet_user('jesse')

print("*"*100)

def display_message():
    print('I am learining about how to define a def()!')
display_message()

print("*"*100)

def favorite_book(book_name):
    print('One of my favorite books is '+book_name+'.')
book_name = 'Alice in Wonderland'
favorite_book(book_name)

favorite_book('Alice')
# book_name是形參,Alice是實參

print("*"*100)

Hello, Jesse!
****************************************************************************************************
I am learining about how to define a def()!
****************************************************************************************************
One of my favorite books is Alice in Wonderland.
One of my favorite books is Alice.
****************************************************************************************************

def describe_pet(pet_name, animal_type='dog'):
    """Display information about a pet."""
    print("\nI have a " + animal_type + ".")
    print("My " + animal_type + "'s name is " + pet_name.title() + ".")
    
# A dog named Willie.
describe_pet('willie')
describe_pet(pet_name='willie')

# A hamster named Harry.
describe_pet('harry', 'hamster')
describe_pet(pet_name='harry', animal_type='hamster')
describe_pet(animal_type='hamster', pet_name='harry')

I have a dog.
My dog's name is Willie.

I have a dog.
My dog's name is Willie.

I have a hamster.
My hamster's name is Harry.

I have a hamster.
My hamster's name is Harry.

I have a hamster.
My hamster's name is Harry.
 

def make_shirt(size='large', printings='I love python'):
    print('The size of this T-Shirt is:' + size + ', and printings is:' + printings + '.')
    
make_shirt('3', 'AAA')
print("*"*100)
make_shirt()
print("*"*100)

def describe_city(city_name='Beijing',country_name='China'):
    print(city_name + ' is in ' + country_name)

describe_city()
print("*"*100)
describe_city(city_name='Shanghai')
print("*"*100)
describe_city('Paries','France')

The size of this T-Shirt is:3, and printings is:AAA.
****************************************************************************************************
The size of this T-Shirt is:large, and printings is:I love python.
****************************************************************************************************
Beijing is in China
****************************************************************************************************
Shanghai is in China
****************************************************************************************************
Paries is in France

def get_formatted_name(first_name, last_name, middle_name=''):
    """Return a full name, neatly formatted."""
    if middle_name:
        full_name = first_name + ' ' +  middle_name + ' ' + last_name
    else:
        full_name = first_name + ' ' + last_name
    return full_name.title()
    
musician = get_formatted_name('jimi', 'hendrix')
print(musician)

musician = get_formatted_name('john', 'hooker', 'lee')
print(musician)

Jimi Hendrix
John Lee Hooker

def build_person(first_name, last_name, age=''):
    """Return a dictionary of information about a person."""
    person = {'first': first_name, 'last': last_name}
    if age:
        person['age'] = age
    return person

musician = build_person('jimi', 'hendrix', age=27)
print(musician)

{'first': 'jimi', 'last': 'hendrix', 'age': 27}

city_country("Beijing", "China")


def make_album(song_name, disc_name='nightwish'):
	music = {'song': song_name, 'disc':disc_name}
	return music

print(make_album('sleeping sun', 'nightwish'))

print(make_album('dead boy'))

while True:
	print('\nPlease tell me the song and the singger and quit by entering "quit"')
	print('\nPlease enter song: ')
	song_name = input('song_name: ')
	if song_name =='quit':
		break
	print('\nPlease enter disc: ')
	disc_name = input('disc_name: ')
	if disc_name == 'quit':	
		break
	aa = make_album(song_name, disc_name)
	print(aa)

"city, country"
{'disc': 'nightwish', 'song': 'sleeping sun'}
{'disc': 'nightwish', 'song': 'dead boy'}
 

magicians = ['zou', 'cheng' , 'li', 'wang']
def show_magicians(magicians_list):
	for magician in magicians_list:
		print(magician)
	print(magicians)


# show_magicians(magicians)


# magicians_list_change = []
# def make_great(magicians_list):
# 	for magician in magicians_list:
# 		magician_change = 'the GREAT ' + magician
# 		magicians_list_change.append(magician_change)
# 		print(magicians_list_change)
# 	print(magicians_list_change)


# make_great(magicians)
# show_magicians(magicians_list_change)

def make_great(magicians_list, changed_magicians_list=[]):
    while magicians_list:
        current_magician = 'the GREAT '+ magicians_list.pop()        
        changed_magicians_list.append(current_magician)
    print(magicians_list)
    print(changed_magicians_list)
        


make_great(magicians[:])
show_magicians(magicians)

[]
['the GREAT wang', 'the GREAT li', 'the GREAT cheng', 'the GREAT zou']
zou
cheng
li
wang
['zou', 'cheng', 'li', 'wang']
 

def make_sandwich(*foods):
	print(foods)
	for food in foods:
		print('You order the sandwich with ' + str(food) +', and it is coming soon!')
make_sandwich('shit', 'milk', 'fruit', 'hell')

print('*'*100)

food_list = ['shit', 'milk', 'fruit', 'hell']
make_sandwich(food_list[0:2],food_list[3],food_list[2])


print('*'*100)


def build_profile(first, last, **user_info):
    """Build a dictionary containing everything we know about a user."""
    profile = {}
    profile['first_name'] = first
    profile['last_name'] = last
    for key, value in user_info.items():
        profile[key] = value
    return profile

user_profile = build_profile('albert', 'einstein',
                             location='princeton',
                             field='physics')
print(user_profile)

print('*'*100)

my_profile = build_profile('zou', 'yi', height='180CM', weight= '200KG')
print(my_profile)

print('*'*100)

def make_car(factory, model, **car_info):
	infor = {}
	infor['factory_name'] = factory
	infor['model_name'] = model
	for key,value in car_info.items():
		infor[key] = value
	print(infor)

make_car('subaru', 'outback', color = 'blue', tow_package = True)
make_car('BMW', 'Fashion', color = 'red', tt = 'what the hell', mm = 'go die')

('shit', 'milk', 'fruit', 'hell')
You order the sandwich with shit, and it is coming soon!
You order the sandwich with milk, and it is coming soon!
You order the sandwich with fruit, and it is coming soon!
You order the sandwich with hell, and it is coming soon!
****************************************************************************************************
(['shit', 'milk'], 'hell', 'fruit')
You order the sandwich with ['shit', 'milk'], and it is coming soon!
You order the sandwich with hell, and it is coming soon!
You order the sandwich with fruit, and it is coming soon!
****************************************************************************************************
{'location': 'princeton', 'last_name': 'einstein', 'first_name': 'albert', 'field': 'physics'}
****************************************************************************************************
{'height': '180CM', 'last_name': 'yi', 'weight': '200KG', 'first_name': 'zou'}
****************************************************************************************************
{'factory_name': 'subaru', 'color': 'blue', 'model_name': 'outback', 'tow_package': True}
{'factory_name': 'BMW', 'tt': 'what the hell', 'model_name': 'Fashion', 'mm': 'go die', 'color': 'red'}

 

 

import pizza
pizza.make_pizza(16,'onion')

print('+'*100)

from pizza import make_pizza
make_pizza(99,'aa','bb','cc','dd')

print('+'*100)

from pizza import make_pizza as mp 
mp(200, 'hell', 'shit', 'bitch')

print('+'*100)

from pizza import *
make_pizza(16,'pepperoni')

print('+'*100)

from printing_models import print_models as pm 

unprinted_designs = ['kk', 'jj', 'hh']
completed_models = []

pm(unprinted_designs, completed_models)

from printing_models import show_completed_models as sm 
sm(completed_models)


Making a 16-inch pizza with the following toppings:
- pepperoni

Making a 12-inch pizza with the following toppings:
- mushrooms
- green peppers
- extra cheese

Making a 16-inch pizza with the following toppings:
- onion
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Making a 99-inch pizza with the following toppings:
- aa
- bb
- cc
- dd
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Making a 200-inch pizza with the following toppings:
- hell
- shit
- bitch
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Making a 16-inch pizza with the following toppings:
- pepperoni
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Printing model: hh
Printing model: jj
Printing model: kk

The following models have been printed:
hh
jj
kk
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章