Exercise Function 3 - python programming from entry to practice

Sandwiches 8-12: write a function that takes the customer to add a series of ingredients in your sandwich. This function takes a single parameter (which collects all the ingredients provided in a function call), and prints a message to sandwich the customer point of an overview. This function is called three times, each providing a different number of arguments.

def adding_foods(*foods):
    print("\nAdd following foods to your sandwich:")
    for food in foods:
        print(food)


adding_foods('corn')
adding_foods('ham', 'corn')
adding_foods('egg', 'beef', 'corn')

8-13 User Profile: copy the previous program user_profile.py, build_profile () to create associated with your profile in which the call; calling this function, specify your first and last name, as well as three describe your key - value pairs.

def build_profile(first, last, **someone_info):
    profile = dict()
    profile['first_name'] = first
    profile['last_name'] = last
    for key, value in someone_info.items():
        profile[key] = value
    return profile


my_profile = build_profile('shirley', 'yang', location="xi'an", age='18', job='test')
print(my_profile)

8-14 car: write a function, the car of messages stored in a dictionary. This function always accept the manufacturer and model, also accepts an arbitrary number of keyword arguments. So call this function: to provide essential information, as well as two name value pairs, such as color and optional accessories. This function must be able to call it with the following:


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


Print dictionary is returned, verify that the correct handling of all information.

def make_car(name, model, **other_info):
    car = dict()
    car['car_name'] = name
    car['car_model'] = model
    for key, value in other_info.items():
        car[key] = value
    return car


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

5-15 print model: the function example print_models.py placed in another file named printing_functions.py in; write an import statement at the beginning of print_models.py and modify the function uses this file to import.

printing_functions.py

def print_models(unprinted_designs, completed_models):
    while unprinted_designs:
        current_design = unprinted_designs.pop()
        print("Printing model: " + current_design)
        completed_models.append(current_design)


def show_completed_models(completed_models):
    print("\nThe following models have been printed:")
    for completed_model in completed_models:
        print(completed_model)
print_models.py

import printing_functions as pf

unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []

pf.print_models(unprinted_designs, completed_models)
pf.show_completed_models(completed_models)

8-16 import: Select a program you write and contains only one function, and this function in another file. In the main program, the following various methods of introducing this function, then call it:


import modul_name

from modul_name import function_name

from modul_name import function_name as fn

import modul_name as mn

from modul_name import *


 

hello_xx.py

def greet_someone(name):
    message = 'Hello, ' + name.title() + '!'
    print(message)
hello_friend.py

import hello_xx
hello_xx.greet_someone("eric")

from hello_xx import greet_someone
greet_someone('lucky')

from hello_xx import greet_someone as gs
gs('babry')

import hello_xx as hx
hx.greet_someone('gre')

from hello_xx import *
greet_someone('coco')

 

Guess you like

Origin www.cnblogs.com/shirley-yang/p/11129054.html