You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 10, 2021. It is now read-only.
1.Python error message: "*SyntaxError: Missing parentheses in call to 'print'*".
1350
+
1.*SyntaxError: Missing parentheses in call to 'print'*
1351
1351
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.
2. Python error message: "*SyntaxError: invalid syntax*". Some of the common causes of this error include:
1357
+
2. *SyntaxError: can't assign to literal*
1360
1358
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.
1362
1360
1363
-
```python
1364
-
if spam ==42# Error on this line
1365
-
print('Hello!')
1366
-
```
1361
+
~~~~ {include="assignment_to_literal"}
1362
+
~~~~
1367
1363
1368
-
b. Using the assignment operator `=` instead of the equality operator `==` inside a condition expression.
1364
+
3. *SyntaxError: invalid syntax*
1369
1365
1370
-
```python
1371
-
if spam = 42: # Error on this line
1372
-
print('Hello!')
1373
-
```
1366
+
Some of the common causes of this error include:
1374
1367
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.
1376
1369
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.
1380
1379
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:
1383
1389
1384
1390
```
1385
-
anddelfromnotwhile
1386
-
aselifglobalorwith
1387
-
assertelseifpassyield
1388
-
breakexceptimportprint
1389
-
classexecinraise
1390
-
continuefinallyisreturn
1391
-
defforlambdatry
1391
+
Falseclass finally isreturn
1392
+
Nonecontinue forlambdatry
1393
+
Truedeffromnonlocal while
1394
+
anddel global not with
1395
+
aselif if or yield
1396
+
assert elseimport pass
1397
+
breakexcept in raise
1392
1398
```
1393
1399
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.
1395
1401
1396
-
```python
1397
-
class='algebra'# Error on this line
1398
-
```
1402
+
~~~~ {include="undefined_operator"}
1403
+
~~~~
1399
1404
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}
1401
1406
1402
-
```python
1403
-
spam=0
1404
-
spam++# Error on this line
1405
-
```
1407
+
a. *IndentationError: unindent does not match any outer indentation level*
1406
1408
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.
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
+
~~~~
1413
1422
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.
1415
1423
1416
-
```python
1417
-
num_even=0
1418
-
num_odd=0
1419
-
for i inrange(100):
1420
-
if i %2==0:
1421
-
num_even +=1
1422
-
else: # Error on this line; five spaces before `else:` instead of four
0 commit comments