zoukankan      html  css  js  c++  java
  • Python:从入门到实践--第八章-函数-练习

    #1.消息:编写一个名为display_message()的函数,它打印一个句子,指出你在本章学的是什么。
    #调用这个函数,确认显示的消息无误
    def display_message(name):
        print(name + "在本章学会了如何调用函数")
    
    display_message('')
    
    #2.喜欢的图书:编写一个名为favorite_book()的函数,其中包含一个名为title的形参
    #用这个函数打印一条消息
    #调用这个函数,并将一本图书的名称作为实参传递给它
    def favorite_book(title):
        print("
    My favorite book is " + title)
    
    favorite_book('《活着》')
    
    print('
    ')
    #3.T恤:编写一个名为make_shirt()的函数,它接受一个尺码以及要印到T恤上的字样
    #这个函数应打印一个句子,概要地说明T恤的尺码和字样
    def make_shirt(size,string):
        print('T恤的尺码为:' + size + ',字样是:' + string)
    
    make_shirt('m','RNG牛逼!')
    
    print('
    ')
    #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 ' + country
        return message.title()
        
    cities = city_country('北京','中国')
    print(cities)
    cities = city_country('纽约','美国')
    print(cities)
    cities = city_country('巴黎','法国')
    print(cities)
    
    
    print('
    ')
    #2.创建一个名为make_album的函数,它创建一个描述音乐专辑的字典。
    #这个函数应该接受歌手名字和专辑名,并返回一个包含这两项信息的字典
    #使用这个函数创建三个不同专辑的字典,并打印每个返回的值,已核实字典正确地存储了专辑的信息
    #给函数make_album()添加一个可选形参,以便能够存储专辑包含的歌曲数
    #如果调用这个函数时指定了歌曲数,就将这个值添加到表示专辑的字典中。调用这个函数,并至少
    #再一次调用中指定专辑包含的歌曲数。
    def make_album(names,albums,number=''):
        person_albums = {'name':names,'album':albums}
        if number:
            person_albums['number'] = number
        return  person_albums
            
    print(make_album('zhangyu','yu'))
    print(make_album('mik','liu',10))
    print(make_album('nike','er'))
    
    
    print('
    ')
    #3.编写一个while循环,让用户输入一个专辑的歌手和名称,获取这些信息后,使用它们来调用函数make_album()
    #并将创建的字典打印出来,在这个while循环中,务必要提供退出路径
    def make_album(names,albums,number=''):
        person_albums = {'name':names,'album':albums}
        if number:
            person_albums['number'] = number
        return  person_albums
    
    print('Enter q to quit anytime ')
    while True:
        singer = input("Enter the name of singer:
    ")
        if singer == 'q':
            break;
        album = input("Enter one of " + singer + "'s album
    ")
        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_magicians
            new_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'] = first
        profile['last_name'] = last
        for key,value in user_info.items():
            profile[key] = value
        return profile
        
    user_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["制造商"] = maker
        car_infos["型号"] = num
        for key,value in car_info.items():
            car_infos[key] = value
        return 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')"""
  • 相关阅读:
    一个简单的PHP登录演示(SESSION版 与 COOKIE版)
    web系统之session劫持解决
    CKFinder 1.4.3 任意文件上传漏洞
    linux服务器磁盘扩容的方法
    Linux下lvm在线扩容步骤
    Centos7使用LVM扩容磁盘(测试成功)
    CentOS7下利用init.d启动脚本实现tomcat开机自启动
    Linux tomcat安装详解(未完)
    linux下 目录(扩容)挂载磁盘
    Linux下环境变量设置
  • 原文地址:https://www.cnblogs.com/geeker-xjl/p/10635274.html
Copyright © 2011-2022 走看看