《Python编程-从入门到实践》第八章习题训练

本文介绍了Python函数的基础知识及其实战应用,包括不同类型的参数传递方式、函数与数据结构的结合使用等。通过多个实例展示了如何定义函数、使用默认参数、接受任意数量的参数等技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本章知识点:

1.编写函数。

2.使用位置实参、关键字实参。

3.接受任意数量的实参。

4.将函数与列表、字典、if语句和while循环结合使用。


习题:

8-1消息

def display_message():
    print("本章我学的是Python中的函数知识")

display_message()

输出:

本章我学的是Python中的函数知识

8-2喜欢的图书

def favourite_book(book):
    print("My favourite book is "+book+".")

favourite_book("Alice in Wonderland")

输出:

My favourite book is Alice in Wonderland.

8-3 T恤

def make_shirt(size,word):
    print('Making this shirt needs '+str(size)+' and '+word+'.')

make_shirt(6,'blod')
make_shirt(size = 6,word = 'blod')

输出:

Making this shirt needs 6 and blod.
Making this shirt needs 6 and blod.

8-4大号T恤

def make_shirt1(size,word = 'blod'):
    print('Making this shirt needs '+str(size)+' and '+word+'.')

make_shirt1(10)
make_shirt1(size = 6)
make_shirt1(5,'grey')

输出:

Making this shirt needs 10 and blod.
Making this shirt needs 6 and blod.
Making this shirt needs 5 and grey.

8-5城市

def describe_city(name,country='China'):
    print(name + ' is in '+country)

describe_city('Li Ming')
describe_city(name = 'Zhang Si')
describe_city(name='Li Si',country='America')

输出:

Li Ming is in China
Zhang Si is in China
Li Si is in America

8-6城市名

def city_country(name,country):
    str = name+', '+country
    return str

print(city_country('Zhang Si','China'))
print(city_country(name='Li Si',country='America'))
print(city_country(country='India',name='Li Ming'))

输出:

Zhang Si, China
Li Si, America
Li Ming, India

8-7专辑

def make_album(name,album_name,num=3):
    music = {'name':name,'album_name':album_name,'number':num}
    return music

print(make_album('Huang','Great'))
print(make_album(name='Zhang',album_name='King',num=5))
print(make_album('Gui','Bian',10))

输出:

{'name': 'Huang', 'album_name': 'Great', 'number': 3}
{'name': 'Zhang', 'album_name': 'King', 'number': 5}
{'name': 'Gui', 'album_name': 'Bian', 'number': 10}

8-8用户的专辑

def make_album1(name,album_name,num=5):
    music = {'name':name,'album_name':album_name,'number':num}
    return music

while True:
    name = input('name:  ')
    album_name = input("album's name:  ")
    if name == 'q':
        break
    tmp = make_album1(name,album_name)
    print(tmp)

输出:

name:  huang
album's name:  great
{'name': 'huang', 'album_name': 'great', 'number': 5}
name:  zhang
album's name:  xin
{'name': 'zhang', 'album_name': 'xin', 'number': 5}
name:  q
album's name:  q

8-9魔术师

def print_chart(names):
    for name in names:
        print('Hello, '+name+', good morning!')

names = ['Hu Nan','Zhang Hong','Lu Jian']
print_chart(names)

输出:

Hello, Hu Nan, good morning!
Hello, Zhang Hong, good morning!
Hello, Lu Jian, good morning!

8-10、8-11了不起的魔术师

def make_great(names):
    tmp = []
    for name in names:
        name = 'The Great '+ name
        tmp.append(name)
    return tmp

def show_magicians(names):
    for name in names:
        print(name)

names = ['Hu Nan','Zhang Hong','Lu Jian']
names1 = make_great(names)
show_magicians(names)
show_magicians(names1)

输出:

Hu Nan
Zhang Hong
Lu Jian
The Great Hu Nan
The Great Zhang Hong
The Great Lu Jian

8-12三明治

def add_material(*food):
    print('The material of food is ')
    print(food)

add_material('eggs')
add_material('eggs','beef')
add_material('eggs','milk','beef')

输出:

The material of food is 
('eggs',)
The material of food is 
('eggs', 'beef')
The material of food is 
('eggs', 'milk', 'beef')

8-13用户简介

def build_profile(first_name,last_name,**infor):
    profile = {}
    profile['first_name'] = first_name
    profile['last_name'] = last_name
    for key,value in infor.items():
        profile[key] = value
    print(profile)

build_profile('Huang','YiKai',Location ='China',Subject='Computer',Hobby='Basketball')

输出:

{'first_name': 'Huang', 'last_name': 'YiKai', 'Location': 'China', 'Subject': 'Computer', 'Hobby': 'Basketball'}

8-14汽车

def build_car(build,size,**infor):
    profile = {}
    profile['build'] = build
    profile['size'] = size
    for key,value in infor.items():
        profile[key] = value
    return profile

tmp = build_car('subaru','outback',color='blue',tow_package = True)
print(tmp)

输出:

{'build': 'subaru', 'size': 'outback', 'color': 'blue', 'tow_package': True}

8-15打印模型

import print_function
print_function.print_information()

输出:

Good Morning!






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值