A Retry Decorator With Arguments
Advanced · Write the code · from 50 Python Practice Problems with Instant Answer Checking (Beginner to Advanced)
From the lesson: 50 Python Practice Problems with Instant Answer Checking (Beginner to Advanced)
Fifty problems, each with a code editor and real tests. Write your solution, press Check, and see exactly which cases pass. Stuck? Hints reveal one at a time, and the solution unlocks once you have checked at least once. Your code and progress are saved in this browser.
Read the lessonWrite retry(times, exceptions=(Exception,)), a decorator factory. The decorated function is called up to times times; if it raises one of exceptions, try again. If the last attempt still fails, re-raise that error. Other exception types must not be retried.
@retry(times=3, exceptions=(ConnectionError,))
def fetch(): ...import functools
def retry(times, exceptions=(Exception,)):
pass
Solution unlocks after your first Check
What Check runs
Your code runs first, then each of these lines. The output must match exactly.
| Runs | Expected output |
|---|---|
| attempts = [] @retry(times=3, exceptions=(ConnectionError,)) def flaky(): attempts.append(1) if len(attempts) < 3: raise ConnectionError("down") return "ok" print(flaky(), len(attempts)) | ok 3 |
| count = [] @retry(times=2) def always_fails(): count.append(1) raise TimeoutError("still down") try: always_fails() except TimeoutError as e: print("gave up after", len(count), "-", e) | gave up after 2 - still down |
More practice problems
- FizzBuzzBeginner
- Reverse the WordsBeginner
- Count the VowelsBeginner
- Palindrome CheckBeginner
- Sum of DigitsBeginner
- Largest Number Without max()Beginner
- Remove Duplicates, Keep the OrderBeginner
- Second Largest ValueBeginner
- Most Common CharacterBeginner
- Are They Anagrams?Beginner
- Factorial, With a Proper ErrorBeginner
- Is It Prime?Beginner
- Letter GradeBeginner
- Title Case Without .title()Beginner
- Common ElementsBeginner
- Flatten a List of ListsBeginner
- Merge Two InventoriesBeginner
- Invert a DictionaryBeginner
- Password Strength CheckBeginner
- Average of Messy InputBeginner
- Fibonacci GeneratorIntermediate
- Sieve of EratosthenesIntermediate
- Caesar CipherIntermediate
- Top Words in a TextIntermediate
- Group the AnagramsIntermediate
- Balanced BracketsIntermediate
- Run-Length EncodingIntermediate
- Binary SearchIntermediate
- Merge Two Sorted ListsIntermediate
- Rotate a ListIntermediate
- Deep FlattenIntermediate
- Roman NumeralsIntermediate
- Longest Common PrefixIntermediate
- Parse a Log Line With RegexIntermediate
- A Bank Account ClassIntermediate
- Two SumAdvanced
- Longest Substring Without RepeatsAdvanced
- Merge Overlapping IntervalsAdvanced
- Top K Frequent ElementsAdvanced
- Maximum Subarray (Kadane)Advanced
- Coin Change (Dynamic Programming)Advanced
- Longest Increasing SubsequenceAdvanced
- Shortest Path in a Graph (BFS)Advanced
- LRU CacheAdvanced
- Trie for AutocompleteAdvanced
- Write a Memoize DecoratorAdvanced
- A Context Manager That Cleans UpAdvanced
- Revenue by Region With pandasAdvanced
- Leaderboard With a DataclassAdvanced