《Python编程从入门到实践》第八章 函数 知识点及习题

本文介绍了多个Python编程练习,涉及函数定义(如display_message,favorite_book,make_shirt等),数据结构(字典和列表使用),以及简单的用户输入处理。通过实例展示了函数的参数传递和控制流的运用。
摘要由CSDN通过智能技术生成

1. 知识点

2. 练习·

# 练习8.1

def display_message():
    print("theme")

display_message()

# 练习8.2

def favorite_book(title):

    print(f"one of my favorite book is {title}")
print("please imput your favorite book")
title = input()
favorite_book(title)

# 练习 8.3

def make_shirt(size,words):
    print(f"shirt's size is {size} and words is \"{words}\" ")

make_shirt("xl","happy")



# 练习 8.4 大号T
def make_shirt(size,words="i ove python"):
    print(f"shirt's size is {size} and words is \"{words}\" ")

make_shirt("xl")
make_shirt("l")
make_shirt("s","happy")

# 练习8.5 城市
def describe_city(name,coutry="China"):

    print(f"{name.title()} is in {coutry.title()}")

describe_city("Xian")
describe_city("hunan")
describe_city("London","England")


def build_person (first_name,last_name,age=None):
    person={'first':first_name, 'last':last_name}
    if age:
        person['age']=age
    return person
musician=build_person('jia','chen','24')
print(musician)

# 练习8.6 城市名
def describe_city(name,coutry):

    print(f"{name.title()} ,  {coutry.title()}")

describe_city("Xian","China")
describe_city("London","England")


# 练习8.7 专辑

def make_album(singer, name, number='None'):
    album={"Singer":singer, 'Name':name}
    if number:
        album['Number']=number

    return album
Album=make_album("vava","cake","9")
print(Album)
Album=make_album("gai","caker","4")
print(Album)


# 练习8.8 用户的专辑
def make_album(singer, name, number='None'):
    album={"Singer":singer, 'Name':name}
    if number:
        album['Number']=number

    return album
while True:
    singer=input("tell me the singer of album\n")
    if singer=='q':
        break
    name=input("tell me the name of album\n")
    if name=='q':
        break
    Album = make_album(singer, name)
    print(Album)

# 练习8.9
messages=['hello','bye','see you','watch out']
send_messages=[]
def show_message(messages,send_messages):

    for message in messages:
       print(message)
       send_messages.append(message)

def send_message(send_messages):
    for send_message in send_messages:
      print(send_message)

show_message(messages[:],send_messages)
send_message(send_messages[:])
# 练习8.10  消息归档

# 练习8.12 三明治
def make_sandwich(*toppings):
    print("Making a sandwich with the following toppings ")
    for topping in toppings:
        print(f"- {topping}")

make_sandwich('tomato','egg','potato')

# 练习8.13 用户简介

def build_profile(first, last,**user_info):
    user_info['first_name']=first
    user_info['last_name']=last
    return user_info

user_profile=build_profile('shao', 'tang', location='tianjing', field=' construction',weigh='210')
print(user_profile)

# 练习8.14 汽车
def make_car(maker,size ,**imformation):
    imformation['Manufacturers']=maker
    imformation['Models']=size
    return imformation

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值