Skip to content

Commit c79312d

Browse files
committed
Added more errors in the "Introduction to Python" chapter.
Because errors are awesome!
1 parent bfcc0d8 commit c79312d

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

python_introduction/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,16 @@ Awesome, right? Of course, variables can be anything, so numbers too! Try this:
148148
>>> a * b
149149
24
150150

151+
But what if we used the wrong name? Can you guess what would happen? Let's try!
152+
153+
>>> name = "Maria"
154+
>>> names
155+
Traceback (most recent call last):
156+
File "<stdin>", line 1, in <module>
157+
NameError: name 'names' is not defined
158+
159+
An error! As you can see, Python has different types of errors and this one is called a **NameError**. Python will give you this error if you try to use a variable that hasn't been defined yet. If you encounter this error later, check your code to see if you've mistyped any names.
160+
151161
Play with this for a while and see what you can do!
152162

153163

@@ -216,6 +226,8 @@ If you want to show only the first number, you can do this by using __indexes__.
216226

217227
As you can see, you can access different objects in your list by using the list's name and the object's index inside of square brackets.
218228

229+
For extra fun, try some other indexes: 6, 7, 1000, -1, -6 or -1000. See if you can predict the result before trying the command. Do the results make sense?
230+
219231
You can find a list of all available list methods in this chapter of the Python documentation: https://docs.python.org/3/tutorial/datastructures.html
220232

221233
## Dictionaries
@@ -327,6 +339,16 @@ You can give Python as many numbers to compare as you want, and it will give you
327339
- __and__ - if you use the `and` operator, both comparisons have to be True in order for the whole command to be True
328340
- __or__ - if you use the `or` operator, only one of the comparisons has to be True in order for the whole command to be True
329341

342+
Have you heard of the expression "comparing apples to oranges"? Let's try the Python equivalent:
343+
344+
>>> 1 > 'django'
345+
Traceback (most recent call last):
346+
File "<stdin>", line 1, in <module>
347+
TypeError: unorderable types: int() > str()
348+
349+
Here you see that just like in the expression, Python is not able to compare a number (`int`) and a string (`str`).
350+
Instead, it shows a **TypeError** and tells us the two types can't be compared together.
351+
330352
## Boolean
331353

332354
Accidently, you just learned about a new type of object in Python. It's called a __Boolean__ -- and it probably is the easiest type there is.
@@ -471,6 +493,15 @@ As you can see, we now gave our function a parameter that we called `name`:
471493

472494
As you can see, we needed to put two spaces before the `print` function, because `if` needs to know what should happen when the condition is met. Let's see how it works now:
473495

496+
>>> hi()
497+
Traceback (most recent call last):
498+
File "<stdin>", line 1, in <module>
499+
TypeError: hi() missing 1 required positional argument: 'name'
500+
501+
Oops, an error. Luckily, Python gives us a pretty useful error message.
502+
It tells us that the function `hi()` (the one we defined) has one required argument (called `name`) and that we forgot to pass it when calling the function.
503+
Let's fix it then:
504+
474505
>>> hi("Ola")
475506
Hi Ola!
476507
>>> hi("Sonja")

0 commit comments

Comments
 (0)