Python Recursion: Vikram & Vetal’s endless stories (no base case?)
- Vinay Borhade

- Sep 10
- 2 min read
Python through the timeless wisdom of Indian Vedic mythology!
By drawing parallels between ancient stories and modern code, programming concepts become much more relatable, fun and easy to remember!
The Legend: Endless Stories on the Graveyard Path
Long ago, King Vikramaditya (Vikram) was tasked with a peculiar challenge—transporting the clever ghost Vetal from a haunted banyan tree to a sage, all while Vetal told him riddling tales. But every time Vikram neared his destination, Vetal would pose a question; if answered, he’d fly back to the tree—forcing Vikram to begin again. Over and over, the cycle repeated: listen, answer, repeat. This seemingly endless loop is one of India’s most beloved story cycles—a perfect fit for understanding recursion!
Understanding Recursion the Vikram-Vetal Way
Recursion in Python is when a function calls itself to solve a smaller piece of a problem. Every recursive call brings us closer to a base case—a condition that finally ends the loop, just as Vikram hoped to eventually break Vetal’s cycle and complete his quest.
Story to Code
Let’s see how King Vikram’s repeated journeys can model recursion in Python:
def vikram_and_vetal_journey(attempt):
if attempt == 0:
print("Vikram has finally solved Vetal's last riddle!")
return
print(f"Vikram answers the riddle. Attempts left: {attempt}")
vikram_and_vetal_journey(attempt - 1)
# Start the journey with 5 attempts
vikram_and_vetal_journey(5)
Output:
text
Vikram answers the riddle. Attempts left: 5
Vikram answers the riddle. Attempts left: 4
Vikram answers the riddle. Attempts left: 3
Vikram answers the riddle. Attempts left: 2
Vikram answers the riddle. Attempts left: 1
Vikram has finally solved Vetal's last riddle!What Makes Recursion Powerful (and Tricky)?
Just as Vikram’s journey repeats the same steps with each story, recursion repeats function calls, each time with a “smaller” problem.
The base case—like Vikram’s final success—prevents the loop from going on forever.
If you forget the base case in code, it’s like Vikram running endlessly in the graveyard—leading to a “RecursionError”!
Wisdom from the Parable
Plan your recursion carefully, always aiming for a base case (“the riddle's answer”).
Too many layers can overwhelm even a strong king—or your computer’s stack!
Recursion is powerful for tasks involving repeated patterns, tree-like structures, and mathematical calculations.
By connecting the clever cycles of Vikram & Vetal with Python’s recursion, you’ll never forget: some problems are best solved by breaking them down, step by step, until the quest is complete!



Comments