Skip to content
This repository was archived by the owner on May 10, 2021. It is now read-only.

Commit 8799418

Browse files
committed
Change the format and unpdate the contents of the 'Syntax Errors' section
1 parent 95647fa commit 8799418

File tree

2 files changed

+72
-59
lines changed

2 files changed

+72
-59
lines changed

filters/includes.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
# Define all possible paths for examples defined to be included in the markdown
1717
# file. For efficiency paths are listed in order of decreasing file amounts.
1818
# Must end in backslash.
19-
PATHS_TO_INCLUDED_CODE = ["../pyta/examples/pylint/", "../pyta/examples/"]
19+
PATHS_TO_INCLUDED_CODE = [
20+
"../pyta/examples/pylint/",
21+
"../pyta/examples/syntax_errors/",
22+
"../pyta/examples/",
23+
]
2024

2125

2226
def _get_file_content(filename):

index.md

Lines changed: 67 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,97 +1347,106 @@ def check(condition, message):
13471347

13481348
### Syntax Error (E0001) {#E0001}
13491349

1350-
1. Python error message: "*SyntaxError: Missing parentheses in call to 'print'*".
1350+
1. *SyntaxError: Missing parentheses in call to 'print'*
13511351

1352-
In Python 3, `print` is a builtin *function*, and should be called like any other function, with arguments inside round brackets. In previous versions of Python, `print` had been a keyword.
1352+
In Python 3, `print` is a builtin *function*, and should be called like any other function, with arguments inside parentheses. In previous versions of Python, `print` had been a keyword.
13531353

1354-
```python
1355-
print "Hello world!" # Error on this line
1356-
print("Hello world!") # Correct version
1357-
```
1354+
~~~~ {include="missing_parentheses_in_call_to_print"}
1355+
~~~~
13581356
1359-
2. Python error message: "*SyntaxError: invalid syntax*". Some of the common causes of this error include:
1357+
2. *SyntaxError: can't assign to literal*
13601358
1361-
a. Forgetting a colon at the end of an `if`, `elif`, `else`, `for`, `while`, `class`, or `def` statement.
1359+
There must always be a variable on the left-hand side of the equals sign (where the term "variable" can refer to a single identifier `a = 10`, multiple identifiers `a, b = 10, 20`, a dictionary element `foo['a'] = 10`, a class attribute `foo.bar = 10`, etc.). You cannot assign to a string or numeric literal.
13621360
1363-
```python
1364-
if spam == 42 # Error on this line
1365-
print('Hello!')
1366-
```
1361+
~~~~ {include="assignment_to_literal"}
1362+
~~~~
13671363
1368-
b. Using the assignment operator `=` instead of the equality operator `==` inside a condition expression.
1364+
3. *SyntaxError: invalid syntax*
13691365
1370-
```python
1371-
if spam = 42: # Error on this line
1372-
print('Hello!')
1373-
```
1366+
Some of the common causes of this error include:
13741367
1375-
c. Forgetting a quote to begin or end a string literal.
1368+
a. Missing colon at the end of an `if`, `elif`, `else`, `for`, `while`, `class`, or `def` statement.
13761369
1377-
```python
1378-
print('Hello!) # Error on this line
1379-
```
1370+
~~~~ {include="missing_colon"}
1371+
~~~~
1372+
1373+
b. Assignment operator `=` used inside a condition expression (likely in place of the equality operator `==`).
1374+
1375+
~~~~ {include="assignment_inside_condition"}
1376+
~~~~
1377+
1378+
c. Missing quote at the beginning or the end of a string literal.
13801379
1381-
d. Trying to use a Python keyword for a variable name.
1382-
Here are all the keywords you should avoid:
1380+
~~~~ {include="missing_quote"}
1381+
~~~~
1382+
1383+
d. Assignment to a Python keyword.
1384+
1385+
~~~~ {include="assignment_to_keyword"}
1386+
~~~~
1387+
1388+
The following is a [list of Python keywords][Keywords] which cannot be used as variable names:
13831389
13841390
```
1385-
and del from not while
1386-
as elif global or with
1387-
assert else if pass yield
1388-
break except import print
1389-
class exec in raise
1390-
continue finally is return
1391-
def for lambda try
1391+
False class finally is return
1392+
None continue for lambda try
1393+
True def from nonlocal while
1394+
and del global not with
1395+
as elif if or yield
1396+
assert else import pass
1397+
break except in raise
13921398
```
13931399
1394-
For example:
1400+
f. Use of an undefined operator. For example, there are no "increment by one" `++` or "decrement by one" `--` operators in Python.
13951401
1396-
```python
1397-
class = 'algebra' # Error on this line
1398-
```
1402+
~~~~ {include="undefined_operator"}
1403+
~~~~
13991404
1400-
e. There is no increment by one `++` or decrement by one `--` operator in Python. The same functionality can be accomplished by writing ` += 1` or ` -= 1`.
1405+
### Indentation Error (E0002) {#E0002}
14011406
1402-
```python
1403-
spam = 0
1404-
spam++ # Error on this line
1405-
```
1407+
a. *IndentationError: unindent does not match any outer indentation level*
14061408
1407-
f. You can't assign to a literal in Python. There must always be a variable name on the left-hand side of the equals sign.
1409+
You must use a constant number of whitespace characters for each level of indentation. If you start a code block using four spaces for indentation, you must use four spaces throughout that code block.
14081410
1409-
```python
1410-
a = 12
1411-
12 = a # Error on this line
1412-
```
1411+
~~~~ {include="unindent_does_not_match_indentation"}
1412+
~~~~
1413+
1414+
Note that it is **strongly recommended** that you [**always use four spaces per indentation level**][PEP8: Indentation] throughout your code.
1415+
1416+
b. *IndentationError: unexpected indent*
1417+
1418+
In Python, the only time you would increase the indentation level of your code is to define a new code block after a [compound statement][Compound statements] such as `for`, `if`, `def`, or `class`.
1419+
1420+
~~~~ {include="unexpected_indent"}
1421+
~~~~
14131422
1414-
g. Unindent does not match any outer indentation level. Make sure that you are always using the same number of spaces to indent your code (typically four). Also make sure that you are never indenting your code with tabs, and replace any existing tabs with four spaces.
14151423
1416-
```python
1417-
num_even = 0
1418-
num_odd = 0
1419-
for i in range(100):
1420-
if i % 2 == 0:
1421-
num_even += 1
1422-
else: # Error on this line; five spaces before `else:` instead of four
1423-
num_odd += 1
1424-
```
14251424
14261425
1427-
# Python objects
1426+
<!-- Python objects -->
14281427
[__init__]: https://docs.python.org/3/reference/datamodel.html#object.__init__
14291428
[str.strip]: https://docs.python.org/3/library/stdtypes.html#str.strip
14301429
[str.lstrip]: https://docs.python.org/3/library/stdtypes.html#str.lstrip
14311430
[str.rstrip]: https://docs.python.org/3/library/stdtypes.html#str.rstrip
14321431
[super]: https://docs.python.org/3/library/functions.html#super
14331432
1434-
# Python docs
1433+
<!-- Python docs -->
14351434
[`pass` statements]: https://docs.python.org/3/tutorial/controlflow.html#pass-statements
1436-
[Binary arithmetic operations]: https://docs.python.org/3/reference/expressions.html#binary-arithmetic-operations
14371435
[Built-in Functions]: https://docs.python.org/3/library/functions.html
1436+
1437+
[Binary arithmetic operations]: https://docs.python.org/3/reference/expressions.html#binary-arithmetic-operations
1438+
[Compound statements]: https://docs.python.org/3/reference/compound_stmts.html
1439+
[Keywords]: https://docs.python.org/3/reference/lexical_analysis.html#keywords
1440+
[Literals]: https://docs.python.org/3/reference/lexical_analysis.html#literals
1441+
[Operators]: https://docs.python.org/3/reference/lexical_analysis.html#operators
1442+
[Delimiters]: https://docs.python.org/3/reference/lexical_analysis.html#delimiters
1443+
14381444
[Unary arithmetic and bitwise operations]: https://docs.python.org/3/reference/expressions.html#unary-arithmetic-and-bitwise-operations
14391445
1440-
# Everything else
1446+
<!-- PEP8 -->
1447+
[PEP8: Indentation]: https://www.python.org/dev/peps/pep-0008/#indentation
1448+
1449+
<!-- everything else -->
14411450
[Common Gotchas - Mutable Default Arguments]: http://python-guide-pt-br.readthedocs.io/en/latest/writing/gotchas/#mutable-default-arguments
14421451
[Default Parameter Values in Python]: http://effbot.org/zone/default-values.htm
14431452
[list comprehensions tutorial]: https://www.digitalocean.com/community/tutorials/understanding-list-comprehensions-in-python-3

0 commit comments

Comments
 (0)