diff --git a/debug-python-errors/README.md b/debug-python-errors/README.md new file mode 100644 index 0000000000..085ee71e00 --- /dev/null +++ b/debug-python-errors/README.md @@ -0,0 +1,3 @@ +# How to Debug Common Python Errors + +This folder provides the code examples for the Real Python tutorial [How to Debug Common Python Errors](https://realpython.com/debug-python-errors/). diff --git a/debug-python-errors/cat.py b/debug-python-errors/cat.py new file mode 100644 index 0000000000..8e350e1ed8 --- /dev/null +++ b/debug-python-errors/cat.py @@ -0,0 +1,3 @@ +cat = "Siamese" + +print(cat) diff --git a/debug-python-errors/fruit.py b/debug-python-errors/fruit.py new file mode 100644 index 0000000000..f63f12b0a8 --- /dev/null +++ b/debug-python-errors/fruit.py @@ -0,0 +1,12 @@ +def capitalize_fruit_names(fruits): + capitalized_fruit_names = [] + cleaned = [fruit if isinstance(fruit, str) else "" for fruit in fruits] + + for fruit in cleaned: + capitalized_fruit_names.append(fruit.capitalize()) + + return capitalized_fruit_names + + +if __name__ == "__main__": + print(capitalize_fruit_names(["apple", "BANANA", "cherry", "maNgo"])) diff --git a/debug-python-errors/palindromes.py b/debug-python-errors/palindromes.py new file mode 100644 index 0000000000..c73d3afa39 --- /dev/null +++ b/debug-python-errors/palindromes.py @@ -0,0 +1,17 @@ +def find_palindromes(text): + # Split sentence into words + words = text.split() + + # Remove punctuation and convert to lowercase + normalized_words = [ + "".join(filter(str.isalnum, word)).lower() for word in words + ] + + # Check for palindromes + return [word for word in normalized_words if word == word[::-1]] + + +if __name__ == "__main__": + print( + find_palindromes("Dad plays many solos at noon, and sees a racecar.") + ) diff --git a/debug-python-errors/test_fruit.py b/debug-python-errors/test_fruit.py new file mode 100644 index 0000000000..83f3520cc4 --- /dev/null +++ b/debug-python-errors/test_fruit.py @@ -0,0 +1,39 @@ +import unittest + +from fruit import capitalize_fruit_names + + +class TestAllFruits(unittest.TestCase): + def test_empty_list(self): + """with empty list""" + self.assertEqual(capitalize_fruit_names([]), []) + + def test_lowercase_list(self): + """with lowercase strings""" + self.assertEqual( + capitalize_fruit_names(["apple", "banana", "cherry"]), + ["Apple", "Banana", "Cherry"], + ) + + def test_uppercase_list(self): + """with uppercase strings""" + self.assertEqual( + capitalize_fruit_names(["APPLE", "BANANA", "CHERRY"]), + ["Apple", "Banana", "Cherry"], + ) + + def test_mixed_case_list(self): + """with mixed case strings""" + self.assertEqual( + capitalize_fruit_names(["mAnGo", "grApE"]), ["Mango", "Grape"] + ) + + def test_non_string_element(self): + """with a mix of integer and string elements""" + self.assertEqual( + capitalize_fruit_names([123, "banana"]), ["", "Banana"] + ) + + +if __name__ == "__main__": + unittest.main()