Python基础-函数-functions1.py
def greet_user():
print("Hello!")
greet_user()
def greet_user2(username):
print("Hello, " + username.title() + "!")
greet_user2('jesse')
def greet_users(names):
for name in names:
print("Hello, " + name.title() + "!")
usernames = ['hannah','ty','margot']
greet_users(usernames)
def describe_pet(animal_type, pet_name):
print("\nI have a " + animal_type + ".")
print("My " + animal_type + "'s name is " + pet_name.title() + ".")
describe_pet('hamster','harry')
describe_pet('dog','willie')
describe_pet(pet_name='harry',animal_type='hamster')
def describe_pet2(pet_name, animal_type='dog'):
print("\nI have a " + animal_type + ".")
print("My " + animal_type + "'s name is " + pet_name.title() + ".")
describe_pet2(pet_name='willie')
describe_pet2('willie')
describe_pet2(pet_name='harry',animal_type='hamster')
def get_formatted_name(first_name,last_name,middle_name=''):
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)
def build_person(first_name,last_name,age=''):
person = {'first':first_name,'last':last_name}
if age:
person['age'] = age
return person
musician = build_person('jimi','hendrix',31)
print(musician)
while True:
print("\nPlease tell me your name:")
print("(Enter 'q' at any time to quit)")
f_name = input("First name: ")
if f_name == 'q':
break
l_name = input("Last name: ")
if l_name == 'q':
break
formatted_name = get_formatted_name(f_name,l_name)
print("\nHello, " + formatted_name + "!")
def confirm_users(unconfirmed_users,confirmed_users):
while unconfirmed_users:
current_user = unconfirmed_users.pop()
print("Verifying user: " + current_user.title())
confirmed_users.append(current_user)
def show_confirmed_users(confirmed_users):
print("\nThe following users have been confirmed:")
for confirmed_user in confirmed_users:
print(confirmed_user.title())
def show_unconfirmed_users(unconfirmed_users):
print("\nThe following users are not confirmed:")
for unconfirmed_user in unconfirmed_users:
print(unconfirmed_user.title())
unconfirmed_users = ['alice','brian','candace']
confirmed_users = []
confirm_users(unconfirmed_users[:],confirmed_users)
show_confirmed_users(confirmed_users)
show_unconfirmed_users(unconfirmed_users)
def make_pizza(*toppings):
print("\nMaking a pizza with the following toppings:")
for topping in toppings:
print("- " + topping)
make_pizza('pepperoni')
make_pizza('mushrooms','green peppers','extra cheese')
def make_pizza(size,*toppings):
print("\nMaking a " + str(size)
+ "-inch pizza with the following toppings:")
for topping in toppings:
print("- " + topping)
make_pizza(16,'pepperoni')
make_pizza(12,'mushrooms','green peppers','extra cheese')
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
user_profile = build_profile('albert','einstein',
location='princeton',
field = 'physics')
print(user_profile)