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
\"Container\" datatypes that can hold many values:
@@ -48,6 +50,7 @@ Basic (\"scalar\") datatypes that involve a single value:
48
50
|[Set](https://docs.python.org/3/tutorial/datastructures.html#sets)| Like a list, but without a particular order, and cannot contain duplicates. |`{5, 3, 1}`|
49
51
|[<spanstyle="white-space:nowrap;">Dictionary</span>](../container-datatypes/dictionaries.qmd)| An object with named attributes, otherwise known as "key value pairs". |`{"first_name": "Michael", "last_name": "Jordan"}`|
Copy file name to clipboardExpand all lines: docs/notes/python-lang/control-flow/errors.qmd
+28-17Lines changed: 28 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -58,10 +58,11 @@ If we find the need to trigger our own errors to stop program execution (less co
58
58
```python
59
59
options = ["rock", "paper", "scissors"]
60
60
61
-
choice ="hoya"# input("Please choose either 'rock', 'paper', or 'scissors': ")
61
+
choice ="hoya"
62
62
63
63
if choice notin options:
64
64
raiseValueError("OOPS - Please type 'rock', or 'paper', or 'scissors'.")
65
+
#> ValueError
65
66
```
66
67
67
68
### Defining and Raising Custom Errors
@@ -73,49 +74,59 @@ class MyCustomError(Exception):
73
74
pass
74
75
75
76
raise MyCustomError("My custom message")
77
+
#> MyCustomError
76
78
```
77
79
78
80
## Common Errors and Explanations
79
81
80
82
### Syntax Error
81
83
82
-
A `SyntaxError` occurs when the code is not written correctly according to the rules of the Python language. This can happen due to missing colons, parentheses, or incorrect indentation.
84
+
A `SyntaxError` occurs when the code is not written correctly according to the rules of the Python language.
85
+
86
+
This can happen due to missing colons, parentheses, or incorrect indentation:
83
87
84
-
Example:
85
88
```python
86
-
# Missing colon
87
-
ifTrue
88
-
print("This will cause a SyntaxError")
89
+
ifTrue# OOPS, MISSING A COLON
90
+
print("Yep")
91
+
92
+
#> SyntaxError
89
93
```
90
94
91
95
### Value Error
92
96
93
-
A `ValueError` occurs when a function receives an argument of the correct type but an inappropriate value. This can happen when trying to convert a string to an integer, but the string does not represent a number.
97
+
A `ValueError` occurs when a function receives an argument of the correct type but an inappropriate value.
98
+
99
+
This can happen when trying to convert a string to an integer, but the string does not represent a number:
94
100
95
101
Example:
96
102
```python
97
-
# Trying to convert a non-numeric string to an integer
98
-
int("hello") # This will cause a ValueError
103
+
int("hello") # OOPS, INVALID CONVERSION
104
+
105
+
#> ValueError
99
106
```
100
107
101
108
### Key Error
102
109
103
-
A `KeyError` occurs when trying to access a dictionary with a key that does not exist. This can happen when trying to retrieve a value from a dictionary using a key that is not present.
110
+
A `KeyError` occurs when trying to access a dictionary with a key that does not exist.
111
+
112
+
This can happen when trying to retrieve a value from a dictionary using a key that is not present:
104
113
105
-
Example:
106
114
```python
107
-
# Accessing a non-existent key in a dictionary
108
115
my_dict = {"name": "Alice"}
109
-
print(my_dict["age"]) # This will cause a KeyError
116
+
print(my_dict["age"]) # OOPS, "AGE" IS NOT A KEY!
117
+
118
+
#> KeyError
110
119
```
111
120
112
121
### Index Error
113
122
114
-
An `IndexError` occurs when trying to access an index that is out of range for a list or other sequence. This can happen when trying to access an element at an index that does not exist.
123
+
An `IndexError` occurs when trying to access an index that is out of range for a list or other sequence.
124
+
125
+
This can happen when trying to access an element at an index that does not exist:
0 commit comments