python中冒号报错_python新手常见错误和异常
在python编程中,新手最常见的错入和异常如下
1.缺少冒号引起的错误
在if,elif,for,while,class,def声明末尾需要添加冒号(:),如果忘记添加,将会提示:“SyntaxError: invalid syntax”语法错误。例如:
1
2
3
4
5
6
>>> if x > 3
print("x > 3 is ture")
File "", line 2
print("x > 3 is ture")
^
SyntaxError: invalid syntax
2.将赋值运算符(=)和比较运算符(==)混淆
如果误将=号用作==号,将会提示“SyntaxError: invalid syntax”语法错误,列如:
1
2
3
4
5
>>> if x = 3:
File "", line 1
if x = 3:
^
SyntaxError: invalid syntax
3.代码结构缩进错误
这是比较常见的错误。当代码结构的缩进不正确时,常常会提示错误信息如:IndentationError: expected an indented block 例如:
1
2
3
4
5
6
7
>>> x = 3
>>> if x > 3 :
... print(" x > 3 is ture")
File "", line 2
print(" x > 3 is ture")
^
IndentationError: expected an indented block
4.修改元组和字符串的值报错
元组和字符串的值是不能修改的,如果修改他们的元素值将会提示错误信息。例如:
1
2
3
4
5
6
>>> tup1 = (12,13)
>>> #以下修改元组元素操作是非法的
... tup1[0] = 100
Traceback (most recent call last):
File "", line 2, in
TypeError: 'tuple' object does not support item assignment
5.连接字符串和非字符串
如果将字符串和非字符串连接,将会提示错误“TypeError: can only concatenate str (not “int”) to str”
1
2
3
4
5
6
>>> s = "I love Pyhton"
>>> m = 18
>>> print(s + m)
Traceback (most recent call last):
File "", line 1, in
TypeError: can only concatenate str (not "int") to str
6.在字符串首尾忘记加引号
字符串的首尾必须添加引号,如果没有添加,或者没有成对出现,则会提示错误“SyntaxError: EOL while scanning string literal”例如:
1
2
3
4
5
>>> print("I love Python)
File "", line 1
print("I love Python)
^
SyntaxError: EOL while scanning string literal
7.变量或者函数名拼写错误
如果函数和变量拼写错误,则会提示错误“NameError: name ‘ab’ is not defined”
1
2
3
4
5
>>> aa = "Learn Python"
>>> print(ab)
Traceback (most recent call last):
File "", line 1, in
NameError: name 'ab' is not defined
8. 应用超过列表的最大索引值
如果引用超过列表的最大索引值,则会提示“IndexError: list index out of range”
1
2
3
4
5
>>> aa = [12,13,14,45 ]
>>> print(aa[4])
Traceback (most recent call last):
File "", line 1, in
IndexError: list index out of range
9.使用关键字作为变量名
Python 关键字不能用作变量名。Python3 的关键字有:[‘False’, ‘None’, ‘True’, ‘and’, ‘as’,
‘assert’, ‘break’, ‘class’, ‘continue’, ‘def’, ‘del’, ‘elif’, ‘else’, ‘except’, ‘finally’,’for’, ‘from’, ‘global’, ‘if’, ‘import’,’in’, ‘is’, ‘lambda’, ‘nonlocal’, ‘not’, ‘or’, ‘pass’, ‘raise’, ‘return’, ‘try’,’while’, ‘with’, ‘yield’]当使用这些关键字作为变量时,将会提示错误“SyntaxError: invalid syntax”例如:
1
2
3
4
5
>>> except = [12,13,13]
File "", line 1
except = [12,13,13]
^
SyntaxError: invalid syntax
10.变量没有初始值就使用增值操作符
如果变量没有指定一个有效的初始值就使用自增操作符,则会提示错误“NameError: name ‘obj’ is not defined”例如:
1
2
3
4
>>> obj += 15
Traceback (most recent call last):
File "", line 1, in
NameError: name 'obj' is not defined
11.误用自增和自减运算符
在python编程语言中,没有自增(++)或者自减(–)运算符。如果误用,则会提示错误“SyntaxError: invalid syntax”例如:
1
2
3
4
5
6
>>> jj = 10
>>> jj ++
File "", line 1
jj ++
^
SyntaxError: invalid syntax
12.忘记为方法的第一个参数添加self参数
在定义方法时,第一个参数必须是self。如果忘记添加self参数,则会提示错误“TypeError: myMethod() takes 0 positional arguments but 1 was given”例如:
1
2
3
4
5
6
7
8
9
>>> class myClass():
... def myMethod():
... print("this is a good method")
...
>>> dd = myClass()
>>> dd.myMethod()
Traceback (most recent call last):
File "", line 1, in
TypeError: myMethod() takes 0 positional arguments but 1 was given
python中冒号报错_python新手常见错误和异常相关推荐
- python中冒号报错_python中一些常见的错误_后端开发
PHP8 新特性之 Attributes_后端开发 PHP8的Alpha版本,过几天就要发布了,其中包含了不少的新特性,今天呢,我想谈谈Attributes,为啥呢, 是昨天我看到很多群在转发一个文章 ...
- python中byte2array报错_python – 使用ByteArrays解压缩Zlib字符串
我有一个用Adobe Flex 3和 Python 2.5开发的Web应用程序(部署在Google App Engine上).已经在Python中创建了一个RESTful Web服务,其结果目前采用X ...
- python中byte2array报错_python基础-bytes和bytearray的用法
Python中的序列类型有bytes和bytearray. 二进制序列类型的用法比较少见,是python中少用的一种序列类型,对于二进制序列类型,大家基本了解即可. bytes二进制序列类型 指定长度 ...
- python写错了怎么撤回_python新手常见错误汇总
对于新手,初学Python时,总会遇到这样那样的报错,想要弄懂Python错误信息的含义可能还不知道怎么做,这里列出了一些比较常见的Python报错问题,希望对于学习Python的人能够有些帮助. 1 ...
- Python运行的17个时新手常见错误小结
Python运行的17个时新手常见错误小结 1 发布时间:『 2017-11-04 11:20 』 帖子类别:『人工智能』 阅读次数:8803 (本文『Python运行的17个时新手常见错误 ...
- python打包exe报错_python 程序打包为 windows 可执行程序 exe
1,使用到的程序 1,python 2,pyinstaller 2,安装 pyinstaller pip install pyinstaller 3,安装可能出现的问题与报错 1:AttributeE ...
- python导入csv报错_Python 导入csv报错的解决办法
日常做分析导数据源进python,有时候会出现报错及显示乱码的问题,今天来梳理一下常见的报错. python 代码 import pandas as pd import numpy as np df= ...
- python双引号报错_Python中select语句中的双引号”“怎么处理
我把MySQL的语句中放在了Python中(我用的Pycharm),有个sql语句是如下:insertinto`level-1metric`(`Year`,`SupplyChain`,`Region` ...
- python后面空格报错_python中空格和table混用报错原因
python是一门严格遵守缩进的语言,缩进的规则代表着程序的层级关系.我们来看一段代码.class MyForm(Form): value1 = StringField('value1') value ...
最新文章
- window.postMessage实现网页间通信
- linux bashrc与profile的区别
- 防止过拟合,采用的手段有哪些?
- 9月数据库排行:Microsoft SQL Server分数罕见下滑
- hihocoder1543 SCI表示法
- 如何在本地设置www.xxx.com,使其允许访问
- [转帖]到底什么是时间复杂度
- 每天1万步就叫健康吗?
- 计算机报名照片无法显示,有关人事考试照片审核处理工具的问题
- 问卷及量表统计与SPSS实战
- React-fiber架构的解释
- TIA博途WINCC中英文切换的项目中摄氏度符号无法正常显示的解决办法
- 三星 GALAXY Note 4 柏林发布会
- 点击链接跳转到微信公众号关注页、微信关注链接。
- 武汉安全员ABC证报名条件有什么要求?甘建二
- 记录 一次 小米路由器4C 刷openwrt 过程
- X264的ARMV7-a的交叉编译及优化运行
- 还原一个真实的马斯克:太空殖民时代的钢铁侠
- 黄一孟:骑着电驴找金矿
- 串口示波器软件NS-Scope
热门文章
- Angular应用ng serve命令行的学习笔记
- Groovy中的任务的自定义属性设置
- 将SAP Analytics Cloud嵌入到SAP Cloud for Customer里去
- 有了Debug权限就能干坏事?小心了,你的一举一动尽在系统监控中
- Hybris Storefront里如何给用户绑定手机号
- bInitiallyDisabled实现原理
- How to resolve Unable to load groups error message
- Cloud Connector的普通版本和Portable版本的区别
- IPRO_DOCXCC_EXTRACT_PARTBODY
- 通过configuration隐藏product overview page上某些区域