Python第四周第二次作业

该章学习函数的使用

8-2 喜欢的图书

def favorite_book(title):
    print("One of my favorite books is " + title + ".")

book = input("Input your favorite book:")
favorite_book(book)

8-4 大号T恤

def make_shirt(size="L", word="I love Python"):
    print('The ' + size + ' shirt prints "' + word + '".')
    
make_shirt()
make_shirt('M')
make_shirt(word="Hello, world!")

8-6 城市名

如果要输出一组单词且每个单词都是首字母大写,用函数组合就很方便了

def city_country(city, country):
    return city.title() + ", " + country.title()
    
print(city_country("Guangzhou", "China"))
print(city_country("London", "England"))
print(city_country("Tokyo", "Japan"))

8-9 魔术师

第一次原列表的值没有被修改,第二次被修改了

Python可以直接使用前面没有被声明过的变量?

def show_magicians(magicians):
    print(magicians)

def make_great(magicians):
    i = 0
    for mag in magicians:
        magicians[i] = "the Great " + magicians[i]
        mags_2.append(magicians[i])
        i += 1
      
mags = ["Alice", "Bob", "Cathy"]
mags_2 = []
show_magicians(mags)

make_great(mags[:])
show_magicians(mags)
show_magicians(mags_2)

mags_2.clear()
make_great(mags)
show_magicians(mags)
show_magicians(mags_2)

8-13 用户简介

def build_profile(first, last, **user_info):
    profile = {}
    profile['first name'] = first
    profile['last name'] = last
    for k, v in user_info.items():
        profile[k] = v
    return profile
    
user_profile = build_profile('S', 'Shr', work='Student', school='SYSU', favorite_language='Phtyon')
print(user_profile)

你可能感兴趣的:(Python第四周第二次作业)