|
启用函数的代码:- 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)
- unprinted_designs = ['iphone case','robot pendant','dodecahedron']
- completed_models=[]
- print_models(unprinted_designs,completed_models)
- show_completed_models(completed_models)
复制代码 不启用函数的代码:- unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
- completed_models = []
- while unprinted_designs:
- current_design = unprinted_designs.pop()
- print("Printing model: " + current_design)
- completed_models.append(current_design)
- print("\nThe following models have been printed:")
- for completed_model in completed_models:
- print(completed_model)
复制代码 以上两段代码的功能和输出结果是完全一样的,如下:- Printing model: dodecahedron
- Printing model: robot pendant
- Printing model: iphone case
- The following models have been printed:
- dodecahedron
- robot pendant
- iphone case
复制代码 由此可以看出,启用了函数以后的代码更有序,更有效率。两个函数各司其职。
|
|