Python常见错误

Python常见语法错误

  • 拼写错误
  • 不正确的缩进
  • 关键字符缺失或不正确:
  • 括号类型 – ( 弯曲 )、[ 方形 ]、{ 卷曲 }
  • 引号类型 – “straight-double”或“straight-single”、“curly-double”或“curly-single”
  • 块介绍字符,例如冒号 – :
  • 数据类型不匹配
  • Python 保留字缺失、使用不正确或位置错误
  • 使用错误的大小写(大写/小写) – Python 是区分大小写的语言

如果您的语法正确,但脚本有意外的行为或输出,这可能是由于语义问题造成的。 语法就像代码的词汇、语法、拼写和标点符号。 语义是编码语句的含义和逻辑。 语法正确的代码可能会成功运行,但不会执行我们希望它执行的操作。

Python常见的语义错误:

  • 创建功能代码,但得到无意的输出
  • 代码设计逻辑结构不佳

在运行代码块时,请注意语法和语义(逻辑)错误以及代码的整体结果。 仅仅因为您修复了错误并不意味着代码在运行时会达到预期的效果! 修复代码中的错误后,不要忘记单击“运行”来检查您的工作。

Common syntax errors:

  • Misspellings
  • Incorrect indentations
  • Missing or incorrect key characters:
    • Bracket types – ( curved ), [ square ], { curly }
    • Quote types – “straight-double” or ‘straight-single’, “curly-double” or ‘curly-single’
    • Block introduction characters, like colons – :
  • Data type mismatches
  • Missing, incorrectly used, or misplaced Python-reserved words
  • Using the wrong case (uppercase/lowercase) – Python is a case-sensitive language 

If your syntax is correct, but the script has unexpected behavior or output, this may be due to a semantic problem. The syntax is like the vocabulary, grammar, spelling, and punctuation of code. Semantics are the meaning and logic of coded statements. It is possible to have syntactically correct code that runs successfully but doesn’t do what we want it to do.

Common semantic errors:

  • Creating functional code, but getting unintentional output
  • Poor logic structures in the design of the code

When working with the code blocks, be mindful of syntax and semantic (logic) errors, along with the overall result of your code. Just because you fixed an error doesn’t mean that the code will have the desired effect when it runs! Once you’ve fixed an error in your code, don’t forget to click Run to check your work.

Share:

You May Also Like