Print Square of Stars Recursively
Print square pattern using recursion.
IntermediatePhase 3 - Level 3: Pattern RecursionExample 2 of 10
print-square-of-stars-recursively.py
1def print_stars(n):2 if n == 0:3 return4 print("*", end="")5 print_stars(n - 1)67def print_square(n, row=0):8 # Base case9 if row == n:10 return1112 # Print row13 print_stars(n)14 print()15 # Recurse for next row16 print_square(n, row + 1)1718# Test19n = int(input("Enter size: "))20print_square(n)
Output
Enter size: 4 **** **** **** ****
What's going on
Print row of n stars, then recurse for next row.
Key Concepts:
Use helper function for printing stars
Print row, then recurse
Track row number with parameter
🔗Related Content
- 📖
Phase 3 - Learn Concepts
Review Phase 3 concepts and explanations
- 📝
Phase 3 - Quiz
Test your Phase 3 understanding with quiz questions
- 💻
Phase 3 - All Practice Problems
Explore all practice problems for Phase 3
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum