Python编程之入门到实践 第八章习题代码

8.1代码习题

# 8-1 消息
def display_message():
    print("在本章中,学会了如何定义函数,什么是实参和形参,如何调用函数")
display_message()


# 8-2 喜欢的图书
def favorite_book(title):
    print(title)
    print("One of my favorite books is Alice in " + title)
favorite_book("asdf")

8.2代码习题

# 8-3 T恤
def make_shirt1(size,world):
    print("you want to get the shirt is :" + size + "\nyou want to print worlds is" + world)

make_shirt1("XL","fighting")

make_shirt1(size='XL', world='fighting')

# 8-4 大号T恤
def make_shirt2(size, world="I love python", age=''):
    print("shirt size is: "+size)
    print("print world is:"+world)
    print(age)

make_shirt2(size="xxl")

make_shirt2(size="xl", world="I love you")

8.3代码习题

# 8-6 city country
def city_country(city, country):
    message = city + "," + country
    return message


information = city_country(city='beijing', country='China')

print(information)


# 8-7 make album
def make_album(siger, album, num=''):
    message = {'siger': siger, 'album': album}
    if num:
        message['num'] = num
    return message


inf = make_album(siger="zhou", album="jin", num="8")
print(inf)


# 8-8 用户的专辑
active = True
while active:
    print("if you want to exit,please input the q")
    singer = input("plaese input the singer: ")
    if singer == "q":
        break
    print("if you want to exit,please input the q")
    album = input("please input the album: ")
    if album == "q":
        break
    message = make_album(singer, album)
    print(message)

8.4代码习题

# 8-9 魔术师
def show_magicians(list):
    for magician in list:
        print(magician)


list1 = ["jake", "bob", "judy", "alex"]
show_magicians(list1)


# 8-10 了不起的魔术师
def make_great(list1, list2):
    while list1:
        inf1 = list1.pop()
        inf2 = "the Great " + inf1
        list2.append(inf2)
    return list2


magicians1 = ["jake", "bob", "judy", "alex"]
magicians2 = []
# messages = make_great(magicians1,magicians2)
# print(messages)

messages = make_great(magicians1[:], magicians2)
print(magicians1)
print(messages)

8.5代码习题

# 8-12 三明治
def make_pizza(*topping):
    print("you want to adding food following topping: ")
    for top in topping:
        print(top)


make_pizza("mushroom","banan","apple")

# 8-13 用户简介
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='jiangxi', field='computer')
print(user_profile)


# 8-14 汽车
def make_car(making, type, **inf):
    profile = {}
    profile["making"] = making
    profile["type"] = type
    for key, value in inf.items():
        profile[key] = value
    return profile


car = make_car('subaru', 'outback', color='blue', tow_package=True)
print(car)

8.6 8.7 代码习题

def make_car(making, type, **inf):
    profile = {}
    profile["making"] = making
    profile["type"] = type
    for key, value in inf.items():
        profile[key] = value
    return profile
# 模块的导入

import pratic6
car = pratic6.make_car('subaru', 'outback', color='blue', tow_package=True)
print(car)

from pratic6 import make_car
car = make_car('subaru', 'outback', color='blue', tow_package=True)
print(car)

from pratic6 import make_car as c1
car = c1('subaru', 'outback', color='blue', tow_package=True)
print(car)

import pratic6 as d1
car = d1.make_car('subaru', 'outback', color='blue', tow_package=True)
print(car)

from pratic6 import *
car = make_car('subaru', 'outback', color='blue', tow_package=True)
print(car)

  • 13
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值