Skip to content

Commit 08be473

Browse files
committed
How to Debug Common Python Errors
1 parent 20a870a commit 08be473

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed

debug-python-errors/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# How to Debug Common Python Errors
2+
3+
This folder provides the code examples for the Real Python tutorial [How to Debug Common Python Errors](https://realpython.com/debug-python-errors/).

debug-python-errors/cat.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
cat = "Siamese"
2+
3+
print(cat)

debug-python-errors/fruit.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def capitalize_fruit_names(fruits):
2+
capitalized_fruit_names = []
3+
cleaned = [fruit if isinstance(fruit, str) else "" for fruit in fruits]
4+
5+
for fruit in cleaned:
6+
capitalized_fruit_names.append(fruit.capitalize())
7+
8+
return capitalized_fruit_names
9+
10+
11+
if __name__ == "__main__":
12+
print(capitalize_fruit_names(["apple", "BANANA", "cherry", "maNgo"]))

debug-python-errors/palindromes.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def find_palindromes(text):
2+
# Split sentence into words
3+
words = text.split()
4+
5+
# Remove punctuation and convert to lowercase
6+
normalized_words = ["".join(filter(str.isalnum, word)).lower() for word in words]
7+
8+
# Check for palindromes
9+
return [word for word in normalized_words if word == word[::-1]]
10+
11+
12+
if __name__ == "__main__":
13+
print(find_palindromes("Dad plays many solos at noon, and sees a racecar."))

debug-python-errors/test_fruit.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import unittest
2+
from fruit import capitalize_fruit_names
3+
4+
class TestAllFruits(unittest.TestCase):
5+
def test_empty_list(self):
6+
"""with empty list"""
7+
self.assertEqual(capitalize_fruit_names([]), [])
8+
9+
def test_lowercase_list(self):
10+
"""with lowercase strings"""
11+
self.assertEqual(
12+
capitalize_fruit_names(["apple", "banana", "cherry"]),
13+
["Apple", "Banana", "Cherry"],
14+
)
15+
16+
def test_uppercase_list(self):
17+
"""with uppercase strings"""
18+
self.assertEqual(
19+
capitalize_fruit_names(["APPLE", "BANANA", "CHERRY"]),
20+
["Apple", "Banana", "Cherry"],
21+
)
22+
23+
def test_mixed_case_list(self):
24+
"""with mixed case strings"""
25+
self.assertEqual(
26+
capitalize_fruit_names(["mAnGo", "grApE"]), ["Mango", "Grape"]
27+
)
28+
29+
def test_non_string_element(self):
30+
"""with a mix of integer and string elements"""
31+
self.assertEqual(
32+
capitalize_fruit_names([123, "banana"]), ["", "Banana"]
33+
)
34+
35+
if __name__ == "__main__":
36+
unittest.main()

0 commit comments

Comments
 (0)