当前位置:   article > 正文

Python编程:从入门到实践 动手试一试 8.1-8.14_python从入门到实践8-14

python从入门到实践8-14

#8.1 messages

def display_message():
    print("In this chapter, you've learned how to use function.")
    
display_message()
  • 1
  • 2
  • 3
  • 4

#8.2 the books I like

def favorite_books(title):
    print("My favorite book is " + title.title() + "!")

favorite_books('call me by your name')
  • 1
  • 2
  • 3
  • 4

#8.3 T-shirt

def make_shirt(size_input, pattern_input):
    print("The T-shirt is about to be " + str(size_input) + " size and " + str(pattern_input) + ".")

make_shirt('L', 'SNAKE')
make_shirt(size_input="M", pattern_input="SNAKE")
  • 1
  • 2
  • 3
  • 4
  • 5

#8.4 big size T-shirt

def make_shirt(size_input, pattern_input="I love Python"):
    print("The T-shirt is about to be " + str(size_input) + " size and " + str(pattern_input) + ".")

make_shirt('L')
make_shirt('M')
make_shirt('XXL', pattern_input="I love Computer")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

#8.5 cities

def describe_cities(name = "Reykjavik", country = "Iceland"):
    print(name.title() + " is in " + country.title())

describe_cities("Shenzhen", "China")
describe_cities('New York', 'U.S.A')
describe_cities()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

#8.6 cities’ name

def city_country(city, country):
    return print(city.title() + ", " + country.title())

city_country("shenzhen", "china")
city_country("shanghai", 'china')
city_country('london','britian')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

#8.7 album

def make_album(name, name_album, number = ""):
    dic = {"Name: ": name.title(), "Name of Album: ": name_album.title(), "Numbers of Songs: ": number}
    return dic

information = make_album(name="benji", name_album="the voice of beauty")
print(information)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

#8.8 users’ album

def make_album_2(name, name_album_2):
    dic_2 = {"Name:": name.title(), "Name of Album:": name_album_2.title()}
    return dic_2

print("Enter 'quit' to stop your search")
while True:
    n = input("Please enter singer's name: ")
    if n == "quit":
        break

    a = input("Please enter album's name: ")
    if a == "quit":
        break

    result = make_album_2(n, a)
    print(result)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

#8.9 magician

names_of_magicians = ['benji', 'simone', 'karl', 'tommy', 'naomi']
def show_magicians():
    for print_name in names_of_magicians:
        print(print_name.title())

show_magicians()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

#8.10 incredible magician

def show_magicians(magician_name_full):
    for magician_name in magician_name_full:
        print(magician_name.title())

def make_great(magician_names,magician_name_full):
    while magician_names:
        current_magician_name = magician_names.pop()
        magician_name_full.append("The Great " + current_magician_name)

magician_names = ['benji', 'simone', 'karl', 'tommy', 'naomi']
magician_name_full = []

make_great(magician_names[:],magician_name_full)
show_magicians(magician_name_full)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

#8.11 unchanged magician

def show_magicians(magician_name_full):
    for magician_name in magician_name_full:
        print(magician_name.title())

def make_great(magician_names,magician_name_full):
    while magician_names:
        current_magician_name = magician_names.pop()
        magician_name_full.append("The Great " + current_magician_name)

magician_names = ['benji', 'simone', 'karl', 'tommy', 'naomi']
magician_name_full = []

make_great(magician_names[:],magician_name_full)
show_magicians(magician_name_full)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

#8.12 sandwich

def sandwich(*toppings):
    print("Making a pizza with the following toppings: ")
    for topping in toppings:
        print("- " + topping)

sandwich('extra cheese', 'beef', 'chicken')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

#8.13 uses’ information

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('Benji', 'He',
                                 location='Shenzhen',
                                 habbit='music', sexuality='male')
print(user_profile)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

#8.14 cars

def make_cars(manufacturer, type, **other):
    car_profile = {}
    car_profile['manufacturer'] = manufacturer
    car_profile['type'] = type
    for key, value in other.items():
        car_profile[key] = value
    return car_profile

car = make_cars('subaru', 'outback', color='blue', tow_package=True)
print(car)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/827720
推荐阅读
相关标签
  

闽ICP备14008679号