首页 分享 python运行文件出现restart

python运行文件出现restart

来源:花匠小妙招 时间:2024-12-21 06:18

ccd9181f78286ae848814049896712fc.png

8.3 返回值

函数并非总是直接显示输出,相反的,它可以处理一些数据,并返回一个或一组值。函数反悔的值被称为返回值。使用return语句将值返回到调用函数的代码行。

8.3.1 返回简单值

def get_formatted_name(first_name,last_name):

full_name=first_name+' '+last_name

return full_name.title()

musician=get_formatted_name('jimi','hendrix')

print(musician)

8.3.2 让实参变成可选的

为了让实参成为可选的,可以给实参制定一个默认值——空字符串,并在用户没有提供中间名时不使用这个实参。此时需要将这个实参放到形参列表的末尾。

def get_formatted_name(first_name,last_name,middle_name=' '):

if middle_name:

full_name=first_name+' '+middle_name+' ' +last_name

else:

full_name=first_name+' ' +last_name

return full_name.title()

musician=get_formatted_name('jimi','hendrix')

print(musician)

musician=get_formatted_name('jimi','hookler','lee')

print(musician)

#运行结果如下:

RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36-32/get_formatted_name.py

Jimi Hendrix

Jimi Lee Hookler

>>>

这里如果没有给实参指定一个默认值的话,就会出现运行错误,如下。。。

RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36-32/get_formatted_name.py

Traceback (most recent call last):

File "C:/Users/Administrator/AppData/Local/Programs/Python/Python36-32/get_formatted_name.py", line 7, in <module>

musician=get_formatted_name('jimi','hendrix')

TypeError: get_formatted_name() missing 1 required positional argument: 'middle_name'

>>>

8.3.3 返回字典

函数可以返回任何类型的值,包括列表和字典等较为复杂的数据结构。

返回字典如下:

def build_person(first_name,last_name):

person={'first':first_name,'last':last_name}

return person

musician=build_person('jimi','hendrix')

print(musician)

#运行结果如下:

RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36-32/person.py

{'first': 'jimi', 'last': 'hendrix'}

>>>

8.4 传递列表

向函数传递列表后,函数就能直接访问其内容,这样使用函数就能够提高处理列表的效率。

def names(nam):

for na in nam:

msg='Hello, '+na.title()+'!'

print(msg)

names_user=['ab','abc','abcd']

names(names_user)

#运行结果如下:

RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36-32/greet_users.py

Hello, Ab!

Hello, Abc!

Hello, Abcd!

在函数中修改列表时,如果需要不改变原有的列表,可以创建一个列表副本,这样函数所做的任何修改都只影响副本,而丝毫不影响原件。

为了达到这个效果,可以使用切片表示法:

[ : ] 创建一个列表副本。

def print_models(unprinted_designs,completed_designs):

while unprinted_designs:

current_design=unprinted_designs.pop()

print('Printing models: '+current_design)

completed_designs.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)

#运行结果如下:

RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36-32/print_models.py

Printing models: dodecahedron

Printing models: robot pendant

Printing models: iphone case

The following models have been printed:

dodecahedron

robot pendant

iphone case

>>>

虽然向函数传递列表的副本可以保留原始列表的内容,但是除非必须,还是应该直接将原始列表传递给函数,因为让函数使用现成列表可以避免花时间和内存创建副本,从而提高效率,在处理大型列表时尤为如此。

8.5 传递任意数量的实参

形参名前加上*号可以让Python创建一个空元组,并将收到的所有值都封存到这个元组中;如果加上两个*号则让Python创建一个空字典,并将受到的所有名称——值对都封装到字典中。

8.6 将函数存储在模块中

函数的优点之一,就是可以使代码块和主程序分离。

所谓模块,就是函数所存储在的独立文件,拓展名为 .py 。可以通过将函数存储在模块中,需要用到时直接将模块导入到主程序中。import语句允许在当前运行的程序中使用模块中的代码。

导入整个模块abc,import abc,之后使用模块abc中的函数mn时,只需abc.mn()

导入模块abc中的特定函数mn,from abc import mn ,之后使用模块abc中的函数mn时,只需mn()

导入模块abc中的所有函数,可以使用*号,让Python导入模块中的所有函数:from abc import *。由于导入了每个函数,可直接通过名称来调用每个函数,而无需使用句号表示法。

在导入函数和模块时,都可以使用as 指定别名。

8.7 函数编写指南

在编写函数时,需要牢记几个细节。

应给函数制定描述性名称,且只在之中使用小写字母和下划线;每个函数都应该包括简要阐述其功能的注释,通过文档字符串的形式实现;如果函数的形参很多时,可在函数定义中输入左括号后按回车键,并在下一行按两次tab键,从而将形参列表和只缩进一层的函数体区分开来。

第九章:类

类表示一类对象都具有的通用行为。在面向对象编程中,通过编写 表示现实世界中的事物和情景的类,并给予这些类来创建对象。

在Python中,首字母大写的名称指的是类。类中的函数称为方法。类中可以通过实例访问的变量称为属性。

class Dog():

def _init_(self,name,age):

self.name=name

self.age=age

def sit(self):

print(self.name.title()+'is now sitting.')

def roll_over(self):

print(self.name.title()+' rolled over!')

my_dog=Dog('willie',6)

print('My dog name is '+my_dog.name.title()+'.')

print('My dog is '+str(my_dog.age)+' year old.')

#一直运行报错!!!!

>>>

RESTART: C:UsersAdministratorAppDataLocalProgramsPythonPython36-32class_dog.py

Traceback (most recent call last):

File "C:UsersAdministratorAppDataLocalProgramsPythonPython36-32class_dog.py", line 9, in <module>

my_dog=Dog('willie',6)

TypeError: object() takes no parameters

>>>

已知的一个错误是:

def _init_(self……)在def 和下划线之间是有空格的。

后面的错误暂未解决!!!

再议。

先到这了,纯小白下次再来!

喜欢梅子酒,喜欢进步的你!

相关知识

Python的简单介绍(一)
RDLC报表 在WinForm里运行出现 未能加载文件或程序集 Microsoft.ReportViewer.WinForms, Version=11.0.0.0 System.IO.FileNotF...
VS Code中code runner运行Python程序[Done] exited with code=null
Python文字花
SKYNE/python
高分毕设:Python农业病虫害问答系统源码
python制作自己的字库
python学习笔记(十六)文件操作
Python画玫瑰花完整代码
Python常见的错误以及其解决方案

网址: python运行文件出现restart https://www.huajiangbk.com/newsview1212800.html

所属分类:花卉
上一篇: 旭日东升
下一篇: 关于<meta name=“vi

推荐分享