【Python系统学习03】错误类型整理(一)

<section id="nice" data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="font-size: 16px; color: black; padding: 10px; line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; word-break: break-word; word-wrap: break-word; text-align: left; font-family: PingFangSC-Light;"><p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;"><a target="_blank" href="https://xingorg1.github.io/xingorg1Note/backEnd/python/03-errorType.html" style="text-decoration: none; word-wrap: break-word; font-weight: bold; color: #ff5722; border-bottom: 1px solid #ff3502; font-family: STHeitiSC-Light;">原网页地址</a></p> </section>

<section id="nice" data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="font-size: 16px; color: black; padding: 10px; line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; word-break: break-word; word-wrap: break-word; text-align: left; font-family: PingFangSC-Light;"><h1 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; border-bottom: 2px solid #ff5722; font-size: 1.3em;"><span style="display: inline-block; font-weight: normal; background: #ff5722; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;">错误类型与可能原因分析</span></h1> <h2 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 24px; text-align: left; margin: 20px 10px 0px 0px;"><span style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid #ff5722;">A、语法错误:</span></h2> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 20px;"><span style="font-size: 14px; color: #ff5722;">1、<code>syntaxError:invalid syntax</code></span></h3> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">无效的语法</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px;">print(<span class="hljs-number" style="color: #d19a66; line-height: 26px;">2019</span>小石头) <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># print(2019小石头)</span><br/> <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># ^</span> <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># SyntaxError: invalid syntax</span> </code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 20px;"><span style="font-size: 14px; color: #ff5722;">2、<code>syntaxError:invalid character in identifier</code></span></h3> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">标识符中有无效的字符</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px;">print(‘我左边的引号是中文的符号<span class="hljs-string" style="color: #98c379; line-height: 26px;">') # print(‘我左边的引号是中文的符号'</span>)<br/> <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># ^</span> <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># SyntaxError: invalid character in identifier</span> </code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;"><strong style="font-weight: border; color: #ff5722;">出错场景:</strong><br> 这通常是因为在 python 中用了中文符号造成的。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">在 Python 中,默认所有正确的语法,包括标点符号都是【英文】。不小心用了中文标点的话,计算机会无法识别,然后报错。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">下边也是这种情况</p> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 20px;"><span style="font-size: 14px; color: #ff5722;">3、<code>SyntaxError: EOL while scanning string literal</code></span></h3> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">扫描字符串文字终止</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px;">print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'我右边的引号是中文的符号‘)

print('</span>我右边的引号是中文的符号‘)<br/>

<span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># ^</span> <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># SyntaxError: EOL while scanning string literal</span> </code></pre>

<h2 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 24px; text-align: left; margin: 20px 10px 0px 0px;"><span style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid #ff5722;">B、类型错误(数据类型不同导致):</span></h2> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 20px;"><span style="font-size: 14px; color: #ff5722;"><code>TypeError: unsupported operand type(s) for ...</code></span></h3> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;"><strong style="font-weight: border; color: #ff5722;">出错场景:</strong><br> 不同类型的数据进行结合计算或处理,就会发生错误。</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px;">a = <span class="hljs-number" style="color: #d19a66; line-height: 26px;">1</span> b = <span class="hljs-string" style="color: #98c379; line-height: 26px;">'1'</span> print(a + b) <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># Traceback (most recent call last):</span><br/> <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># print(a + b)</span><br/> <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># TypeError: unsupported operand type(s) for +: 'int' and 'str'</span> </code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">Python 是强类型语言,不同类型的数据之间需要进行类型转换才能一起“共事”。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">比如,<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(271,93,108);">1+'1'</code>这么写,在 js 中肯定没问题,因为会进行类型转换,把数字 1 变成字符串 1,然后就变成了字符串拼接,最后的到 11。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">但是 python 不会进行隐式类型转换,他发现你用数字 1+字符串 1,就会报类型错误,也就是 syntaxError:invalid syntax,告诉你这么写是无效的。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">改正:</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px;">a = <span class="hljs-number" style="color: #d19a66; line-height: 26px;">1</span> b = <span class="hljs-string" style="color: #98c379; line-height: 26px;">'1'</span><br/> <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># print(a + b)</span><br/> <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># # 这么写报错。改正如下:</span><br/> print(a + int(b))<br/> <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># 2,将字符串转换为int类型,进行加法运算</span><br/> print(str(a) + b)<br/> <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># 11,将整数转为字符串类型,进行字符串拼接。</span> </code></pre> <h2 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 24px; text-align: left; margin: 20px 10px 0px 0px;"><span style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid #ff5722;">C、名称错误(变量未声明就使用):</span></h2> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 20px;"><span style="font-size: 14px; color: #ff5722;"><code>NameError: name 'xingorg1' is not defined</code></span></h3> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;"><strong style="font-weight: border; color: #ff5722;">出错场景:</strong><br> 直接使用一个没有声明的变量,当在本作用域和全局作用域中找不到时,就会发生错误。</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px;">print(xingorg1)<br/> <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># print(xingorg1)</span><br/> <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># NameError: name 'xingorg1' is not defined</span> </code></pre> <h2 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 24px; text-align: left; margin: 20px 10px 0px 0px;"><span style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid #ff5722;">D、值异常</span></h2> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 20px;"><span style="font-size: 14px; color: #ff5722;"><code>ValueError: invalid literal for int() with base 10: '1.8'</code></span></h3> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;"><strong style="font-weight: border; color: #ff5722;">出错场景:</strong><br> Python 的语法规则,浮点类型的字符串不能使用 int()函数进行强制转换。</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px;">print(int(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'1.8'</span>))<br/> <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># print(int('1.8'))</span><br/> <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># ValueError: invalid literal for int() with base 10: '1.8'</span> </code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;"><strong style="font-weight: border; color: #ff5722;">解决方案</strong><br> 虽然浮点形式的字符串,不能使用 int()函数。但浮点数是可以被 int()函数强制转换的</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">可以先将字符串转换为浮点类型,再将浮点数转换为 int 类型。如下:</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px;">print(int(float(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'1.8'</span>)))<br/> <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># 1</span> </code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">不过对下面这种值异常的情况就无计可施了:</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px;">print(int(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'非整数数字字符串'</span>))<br/> <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># print(int('非整数数字字符串'))</span><br/> <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># ValueError: invalid literal for int() with base 10: '非整数数字字符串'</span> </code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">纯文字类数据,无法转换为整数类型。</p> <h2 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 24px; text-align: left; margin: 20px 10px 0px 0px;"><span style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid #ff5722;">E、缩进错误</span></h2> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 20px;"><span style="font-size: 14px; color: #ff5722;"><code>IndentationError: expected an indented block</code></span></h3> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;"><strong style="font-weight: border; color: #ff5722;">出错场景:</strong><br> 对于 Python 而言,冒号和缩进是一种语法。它会帮助 Python 区分代码之间的层次,理解条件执行的逻辑及先后顺序。</p> <blockquote data-tool="mdnice编辑器" style="display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; padding-top: 10px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; margin-bottom: 20px; margin-top: 20px; font-style: normal; border-left: none; padding: 10px; position: relative; line-height: 1.8; border-radius: 0px 0px 10px 10px; color: #333; background: #f5f5f5; box-shadow: #eaeaea 0px 10px 15px;"><span style="display: inline; color: #cecece; font-size: 3em; float: left; line-height: 1em; font-weight: 500;">“</span> <p style="padding-top: 8px; padding-bottom: 8px; letter-spacing: 0.2em; word-spacing: 0.1em; margin: 0px; line-height: 26px; color: #333; font-size: 13px; display: inline;">【注:缩进是四个空格或一个 Tab 键】</p> <span style="float: right; display: inline; color: #cecece; font-size: 3em; line-height: 1em; font-weight: 500;">”</span></blockquote> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">在语句代码中的冒号“:”后、下一行内容的前面,要有缩进,空几个格。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">如果在需要缩进的语句下边没有缩进的代码块,就会报错。</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px;">number = <span class="hljs-number" style="color: #d19a66; line-height: 26px;">1</span> <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">if</span> number==<span class="hljs-string" style="color: #98c379; line-height: 26px;">'1'</span>: print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'1'</span>)<br/> <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># File "main.py", line 3</span><br/> <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># print('1')</span><br/> <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># ^</span><br/> <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># IndentationError: expected an indented block</span> </code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">这是因为当我们去掉缩进时,条件/语句(上边的 if)会和需要执行的命令(上边的 print)成为了两个不同的代码组,属于平行关系。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">if 条件下面,缺少了可以执行的动作。那么无论条件成不成立,都不会执行什么操作,这个条件的存在没有任何意义。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;"><strong style="font-weight: border; color: #ff5722;">解决写法:</strong><br> 加缩进</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px;">number = <span class="hljs-number" style="color: #d19a66; line-height: 26px;">1</span><br/> <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">if</span> number==<span class="hljs-string" style="color: #98c379; line-height: 26px;">'1'</span>: print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'1'</span>) <br/><span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># 注意if下边需要执行的命令必须向右缩进</span>

</code></pre>

</section>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章