Print Line of N Stars Recursively
Print n stars in a line using recursion.
IntermediatePhase 3 - Level 3: Pattern RecursionExample 1 of 10
print-line-of-n-stars-recursively.py
1def print_stars(n):2 # Base case3 if n == 0:4 return56 # Recursive case7 print("*", end="")8 print_stars(n - 1)910# Test11n = int(input("Enter n: "))12print_stars(n)13print()
Output
Enter n: 5 *****
What's going on
Print one star, then recurse for n-1 stars.
Key Concepts:
Base case: n == 0, return
Print one star
Recurse with n-1
🔗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