热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Python:从入门到实践第八章函数练习

#1.消息:编写一个名为display_message()的函数,它打印一个句子,指出你在本章学的是什么。#调用这个函数,

#1.消息:编写一个名为display_message()的函数,它打印一个句子,指出你在本章学的是什么。
#调用这个函数,确认显示的消息无误
def display_message(name):print(name
+ "在本章学会了如何调用函数")display_message('')#2.喜欢的图书:编写一个名为favorite_book()的函数,其中包含一个名为title的形参
#用这个函数打印一条消息
#调用这个函数,并将一本图书的名称作为实参传递给它
def favorite_book(title):print(
"\nMy favorite book is " + title)favorite_book('《活着》')print('\n')
#
3.T恤:编写一个名为make_shirt()的函数,它接受一个尺码以及要印到T恤上的字样
#这个函数应打印一个句子,概要地说明T恤的尺码和字样
def make_shirt(size,
string):print('T恤的尺码为:' + size + ',字样是:' + string)make_shirt('m','RNG牛逼!')print('\n')
#
4.修改make_shirt()函数,使其在默认情况下制作一件印有‘I love Python'的字样
def make_shirt(size,string='I love python'):print('T恤的尺码为:' + size + ',字样是:' + string)make_shirt('m')
make_shirt(
's','I love china')#1.编写一个名为city_country()的函数,它接受城市的名称及其所属的国家
#这个函数应返回这样的字符串’santigo Chile‘def city_country(city,country):message
= city +' belong to ' + countryreturn message.title()cities = city_country('北京','中国')
print(cities)
cities
= city_country('纽约','美国')
print(cities)
cities
= city_country('巴黎','法国')
print(cities)print(
'\n')
#
2.创建一个名为make_album的函数,它创建一个描述音乐专辑的字典。
#这个函数应该接受歌手名字和专辑名,并返回一个包含这两项信息的字典
#使用这个函数创建三个不同专辑的字典,并打印每个返回的值,已核实字典正确地存储了专辑的信息
#给函数make_album()添加一个可选形参,以便能够存储专辑包含的歌曲数
#如果调用这个函数时指定了歌曲数,就将这个值添加到表示专辑的字典中。调用这个函数,并至少
#再一次调用中指定专辑包含的歌曲数。
def make_album(names,albums,number
=''):person_albums = {'name':names,'album':albums}if number:person_albums['number'] = numberreturn person_albumsprint(make_album('zhangyu','yu'))
print(make_album(
'mik','liu',10))
print(make_album(
'nike','er'))print('\n')
#
3.编写一个while循环,让用户输入一个专辑的歌手和名称,获取这些信息后,使用它们来调用函数make_album()
#并将创建的字典打印出来,在这个while循环中,务必要提供退出路径
def make_album(names,albums,number
=''):person_albums = {'name':names,'album':albums}if number:person_albums['number'] = numberreturn person_albumsprint('Enter q to quit anytime ')
while True:singer = input("Enter the name of singer:\n")if singer == 'q':break;album = input("Enter one of " + singer + "'s album\n")if album == 'q':break;print(make_album(singer,album))#1.魔术师:创建一个包含魔术师名字的列表,并将其传递给一个名为show_magicians()的函数
#这个函数打印列表中每个魔术师的名字
"""
magicians_names = ['刘谦','大卫科菲尔','黑羽快斗']
def show_magicians(names):print(
"Magicians' names:")for name in names:print(name)show_magicians(magicians_names)
"""
#2.在1中的程序,编写一个名为make_great()的函数,对魔术师列表进行修改,
#在每个魔术师的名字中都加入字样‘the Great’。调用函数show_magicians(),确认魔术师列表确实变了magicians
= ['刘谦','大卫科菲尔','黑羽快斗']
new_magicians
= []def make_great(magicians,new_magicians):while magicians:current_magicians = magicians.pop()current_magicians = 'The Great ' + current_magiciansnew_magicians.append(current_magicians)def show_magicians(new_magicians):print("Magicians' names:")for name in new_magicians:print(name)make_great(magicians[:],new_magicians)
show_magicians(magicians)
show_magicians(new_magicians)#
1.编写一个函数,它接受顾客要在三明治中添加的一系列食材。
#这个函数只有一个形参(他搜集函数调用中提供的所有食材),并打印一条消息对顾客点的三明治进行概述
#调用这个函数三次,每次提供的不同数量的实参def make_sandwich(
*toppings):print("The toppings of sandwich: ")print(toppings)#make_sandwich('banana')
#make_sandwich(
'apple','strawberry','candy')#2.用户简介:调用函数指定你的名和姓,以及三个描述你的键值对
"""
def build_profile(first,last,**user_info):profile = {}profile['first_name'] = firstprofile['last_name'] = lastfor key,value in user_info.items():profile[key] = valuereturn profileuser_profile = build_profile('long','xiao',location='yantai', field='IT')
print(user_profile)
"""
#3.汽车:编写一个函数,将一辆汽车的信息存储在一个字典中。这个函数总是接受制造商和型号
#还接受任意数量的关键字实参。这样调用这个函数:提供必不可少的信息,以及两个名称——值对
#如颜色和选装配件。这个函数必须能够像这样进行调用:car
= make_car('subaru','outback',color='blue',two_package=True)def make_car(maker,num,**car_info):car_infos = {}car_infos["制造商"] = makercar_infos["型号"] = numfor key,value in car_info.items():car_infos[key] = valuereturn car_infos#car = make_car('subaru','outback',color='yellow',two_package=True)
#print(car)#
1.导入模块的练习"""
import parameter_delivery
parameter_delivery.make_sandwich(
'candy','apple','banana')
car_info
= parameter_delivery.make_car('bwm','98k',color='red',price='100万')
print(car_info)
"""

"""
from parameter_delivery import make_sandwich
make_sandwich(
'sala','milk')
"""

"""
from parameter_delivery import make_sandwich as ms
ms(
'strawberry','applr')
"""

"""
import parameter_delivery as pd
pd.make_sandwich(
'hahaha','lalala')
"""

"""
from parameter_delivery import *
car_info
= make_car('bwn','98k')
print(car_info)
make_sandwich(
'666','250')
"""
#1.导入模块的练习
"""import parameter_deliveryparameter_delivery.make_sandwich('candy','apple','banana')car_info = parameter_delivery.make_car('bwm','98k',color='red',price='100万')print(car_info)"""
"""from parameter_delivery import make_sandwichmake_sandwich('sala','milk')"""
"""from parameter_delivery import make_sandwich as msms('strawberry','applr')"""
"""import parameter_delivery as pdpd.make_sandwich('hahaha','lalala')"""
"""from parameter_delivery import *car_info = make_car('bwn','98k')print(car_info)make_sandwich('666','250')"""

 

转:https://www.cnblogs.com/geeker-xjl/p/10635274.html



推荐阅读
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • Java 中的 BigDecimal pow()方法,示例 ... [详细]
  • 本文介绍了Java并发库中的阻塞队列(BlockingQueue)及其典型应用场景。通过具体实例,展示了如何利用LinkedBlockingQueue实现线程间高效、安全的数据传递,并结合线程池和原子类优化性能。 ... [详细]
  • Docker的安全基准
    nsitionalENhttp:www.w3.orgTRxhtml1DTDxhtml1-transitional.dtd ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 本文介绍如何利用动态规划算法解决经典的0-1背包问题。通过具体实例和代码实现,详细解释了在给定容量的背包中选择若干物品以最大化总价值的过程。 ... [详细]
  • QUIC协议:快速UDP互联网连接
    QUIC(Quick UDP Internet Connections)是谷歌开发的一种旨在提高网络性能和安全性的传输层协议。它基于UDP,并结合了TLS级别的安全性,提供了更高效、更可靠的互联网通信方式。 ... [详细]
  • PyCharm下载与安装指南
    本文详细介绍如何从官方渠道下载并安装PyCharm集成开发环境(IDE),涵盖Windows、macOS和Linux系统,同时提供详细的安装步骤及配置建议。 ... [详细]
  • 探讨如何高效使用FastJSON进行JSON数据解析,特别是从复杂嵌套结构中提取特定字段值的方法。 ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 本文基于刘洪波老师的《英文词根词缀精讲》,深入探讨了多个重要词根词缀的起源及其相关词汇,帮助读者更好地理解和记忆英语单词。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 数据管理权威指南:《DAMA-DMBOK2 数据管理知识体系》
    本书提供了全面的数据管理职能、术语和最佳实践方法的标准行业解释,构建了数据管理的总体框架,为数据管理的发展奠定了坚实的理论基础。适合各类数据管理专业人士和相关领域的从业人员。 ... [详细]
author-avatar
kei_herme
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有