Python编程从入门到实践第八章-函数

8-1

def display():
    print("I will learn function in this chapter")#函数 function chapter章节
display()

8-2

def favorite_book(title):
    print("One of my favorite book is " + title.title() + ".")
favorite_book(‘sword coming‘)

8-3

def make_shirt(size,style):
    print("The T-shirt‘s size is " + str(size) +",and style is " + style + ".")
make_shirt("22","Letter‘SB‘")

8-4

def make_shirt(size,style=‘I love Python‘):
    print("The T-shirt‘s size is " + size +",and style is " + style + ".")
make_shirt(‘big‘)
make_shirt(‘middle‘,‘I love c#‘)
输出:
The T-shirt‘s size is big,and style is I love Python.
The T-shirt‘s size is middle,and style is I love c#.

8-5

def city_coutry(city,country=‘China‘):
    print(city + " is in " + country + ".")
city_coutry(‘beijing‘)
city_coutry(‘hebei‘)
city_coutry(‘pairs‘,‘France‘)
输出:
beijing is in China.
hebei is in China.
pairs is in France.

8-6   太垃圾了。。

def city_coutry(city,country):
    inp = city + ‘,‘ + country
    return inp.title()
citys = city_coutry(‘beijing‘,‘china‘)
print(citys)
citys = city_coutry(‘shanghai‘,‘china‘)
print(citys)
citys = city_coutry(‘shenzhen‘,‘china‘)
print(citys)

8-7

def make_album(singer_name,album_name,song_num=‘‘):
    if song_num:
        makea = {‘singer‘:singer_name,‘album‘:album_name,‘num‘:song_num}
    else:
        makea = {‘singer‘:singer_name,‘album‘:album_name}
    return makea
makeal = make_album(‘wugu‘,‘caoge‘,‘15‘)
print(makeal)
makeal = make_album(‘woyebuxiangzheyang‘,‘liujiaying‘)
print(makeal)

8-8

def make_album(singer_name,album_name,song_num=‘‘):
    makea = {‘singer‘:singer_name,‘album‘:album_name,‘num‘:song_num}
    return makea
while True:
    print("\npPlease enter the name of singer and album you like:")
    print("enter ‘q‘ to quit!")
    singer_name = input("singer_name:")
    if singer_name == ‘q‘:
        break
    album_name = input("album_name:")
    if album_name == ‘q‘:
        break
输出:
pPlease enter the name of singer and album you like:
enter ‘q‘ to quit!
singer_name:liujiaying
album_name:woyebuxiangzheyang

pPlease enter the name of singer and album you like:
enter ‘q‘ to quit!
singer_name:q

8-9

def show_magicans(names):
    for name in names:
        print(name)
magican_name = (‘ayya‘,‘xixi‘,‘wuwu‘)
show_magicans(magican_name)
输出:
ayya
xixi
wuwu

8-10照着书上一步步来的,,不太明白意义

magican_names = [‘ayya‘,‘xixi‘,‘wuwu‘]
great_magicans = []
def make_great(magican_names,great_magicans):
    while magican_names:
        magican_name = magican_names.pop()
        great_magican = ‘the great ‘ + magican_name
        great_magicans.append(great_magican)
def show_magicans(great_magicans):
      for name in great_magicans:
            print(name)
make_great(magican_names,great_magicans)
show_magicans(great_magicans)
输出;
the great wuwu
the great xixi
the great ayya

8-11  在8-10 中, 倒数第二行改成make_great(magican_names[:],great_magicans)           验证结果 传给show_magicans() magican_names即可

8-12

def pizza_ins(*toppings):
    print(toppings)
pizza_ins(‘pingguo‘,‘putao‘,‘liulian‘)

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(‘yu‘,‘zhang‘,
                                age =‘22‘,sex =‘man‘)
print(user_profile)

8-14   就改了几个词。。

def make_car(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
car = make_car(‘sabru‘,‘outback‘,
                                color =‘blue‘,tow_package =‘True‘)
print(car)

8-15 略

鸽了3天..

原文地址:https://www.cnblogs.com/zhangyueba/p/12244026.html

时间: 2024-07-31 02:49:43

Python编程从入门到实践第八章-函数的相关文章

Python编程从入门到实践(第三、四章的列表和元祖) 𢪿

原文: http://blog.gqylpy.com/gqy/414 置顶:来自一名75后老程序员的武林秘籍--必读(博主推荐) 来,先呈上武林秘籍链接:http://blog.gqylpy.com/gqy/401/ 你好,我是一名极客!一个 75 后的老工程师! 我将花两分钟,表述清楚我让你读这段文字的目的! 如果你看过武侠小说,你可以把这个经历理解为,你失足落入一个山洞遇到了一位垂暮的老者!而这位老者打算传你一套武功秘籍! 没错,我就是这个老者! 干研发 20 多年了!我也年轻过,奋斗过!我

Python编程从入门到实践(基础入门) 򝻨

原文: http://blog.gqylpy.com/gqy/468 置顶:来自一名75后老程序员的武林秘籍--必读(博主推荐) 来,先呈上武林秘籍链接:http://blog.gqylpy.com/gqy/401/ 你好,我是一名极客!一个 75 后的老工程师! 我将花两分钟,表述清楚我让你读这段文字的目的! 如果你看过武侠小说,你可以把这个经历理解为,你失足落入一个山洞遇到了一位垂暮的老者!而这位老者打算传你一套武功秘籍! 没错,我就是这个老者! 干研发 20 多年了!我也年轻过,奋斗过!我

《Python编程从入门到实践》_第十章_文件和异常

读取整个文件 文件pi_digits.txt #文件pi_digits.txt 3.1415926535 8979323846 2643383279 下面的程序打开并读取整个文件,再将其内容显示到屏幕中: with open("pi_digits.txt") as fileobject: contents = fileobject.read() print(contents) #运行结果 3.1415926535 8979323846 2643383279 使用函数open()打开文件

分享 《Python编程从入门到实践》+PDF+源码+EricMatthes+袁国忠

下载:https://pan.baidu.com/s/1pUlPpTMnffNgMfovUbZkJg 更多资料分享:http://blog.51cto.com/14087171 Python编程从入门到实践(高清中文版PDF+高清英文版PDF+源代码) 久负盛名的python入门书籍. 高清中文版462页,带目录和书签,文字可以复制粘贴: 高清英文版562页,带目录和书签,文字可以复制粘贴: 中文和英文两版对比学习: 讲解详细并配有源代码. 其中,高清中文版如图: 原文地址:http://blo

《矩阵分析与应用(第2版)张贤达》PDF+《Python编程从入门到实践》中英文PDF+源代码

下载:https://pan.baidu.com/s/1R8hwv-PZ7DYsl_gUtLWrag <矩阵分析与应用(第二版)张贤达>PDF 下载:https://pan.baidu.com/s/1hxfM_cL7hGpz7baorzuTjw <Python编程从入门到实践>(高清中文版PDF+高清英文版PDF+源代码) 下载:https://pan.baidu.com/s/1oRGp4_LfDVLo86r79pnXvg <凸优化>中文版PDF+英文版PDF+习题题解

《Python编程从入门到实践》+《流畅的Python》+《Python基础教程(第3版)》分析对比

<Python编程从入门到实践>针对所有层次的Python 读者而作的Python 入门书.全书分两部分:第一部分介绍用Python 编程所必须了解的基本概念,包括matplotlib.NumPy 和Pygal 等强大的Python 库和工具介绍,以及列表.字典.if 语句.类.文件与异常.代码测试等内容:第二部分将理论付诸实践,讲解如何开发三个项目,包括简单的Python 2D 游戏开发如何利用数据生成交互式的信息图,以及创建和定制简单的Web 应用,并帮读者解决常见编程问题和困惑. <

《Python编程从入门到实践》(高清中文版PDF+高清英文版PDF+源代码)

<Python编程从入门到实践>(高清中文版PDF+高清英文版PDF+源代码)确实是非常适合初学者入门的python神书,文笔精炼,内容浅显易懂,还有源代码可做书里的练习题,找到了高清版和大家分享 https://pan.baidu.com/s/15OxbsPVHKFaBXfGy94l6BQ,无提取码 原文地址:https://blog.51cto.com/14218102/2355881

Python编程 从入门到实践 PDF 下载

网盘下载:Python编程 从入门到实践 PDF 下载 – 易分享电子书PDF资源网 作者: [美]埃里克·马瑟斯 出版社: 人民邮电出版社 副标题: 从入门到实践 原作名: Python Crash Course 译者: 袁国忠 出版年: 2016-7-1 页数: 459 定价: CNY 89.00 装帧: 平装 内容简介 · · · · · · 本书是一本针对所有层次的Python 读者而作的Python 入门书.全书分两部分:第一部分介绍用Python 编程所必须了解的基本概念,包括mat

【PDF下载】Python编程 从入门到实践 PDF 下载

网盘下载:Python编程 从入门到实践 PDF 下载 – 易分享电子书PDF资源网 作者: [美]埃里克·马瑟斯 出版社: 人民邮电出版社 副标题: 从入门到实践 原作名: Python Crash Course 译者: 袁国忠 出版年: 2016-7-1 页数: 459 定价: CNY 89.00 装帧: 平装 内容简介 · · · · · · 本书是一本针对所有层次的Python 读者而作的Python 入门书.全书分两部分:第一部分介绍用Python 编程所必须了解的基本概念,包括mat