Character Patterns Recursively
Build alphabet rows with two recursive helpers. Students trace row counts, per-row character positions, and the base cases that bound both recursive paths.
1def print_chars(n, current=0):2 # Base case3 if current >= n:4 return56 # Print character7 print(chr(65 + current), end="") # 65 is 'A'8 # Recurse9 print_chars(n, current + 1)1011def print_pattern(n, row=1):12 # Base case13 if row > n:14 return1516 # Print row17 print_chars(row)18 print()19 # Recurse20 print_pattern(n, row + 1)2122# Test23n = int(input("Enter rows: "))24print_pattern(n)
Output
Enter rows: 4 A AB ABC ABCD
In this one
- Recurse over character codes
- Reset per-row positions
- Coordinate two helpers
How it works
- 1
Choose a row width in print_pattern.
- 2
Print chr(65 + current) until current reaches that width.
- 3
End the row and request the next one.
Edge cases
What happens for n = 0?
What happens for n = 0?
The pattern helper prints no rows.
Does the character sequence wrap after Z?
Does the character sequence wrap after Z?
No. chr continues to later code points.
What's going on
print_chars converts 65 + current with chr, beginning at A. Its current counter rises until it reaches the requested row width.
print_pattern calls that helper for widths one through n and ends each line before requesting the next row. Thus three rows are A, AB, ABC. The call stack tracks both helpers. No alphabet wrap is coded after Z.
What happens for n = 0? The pattern helper prints no rows.
Does the character sequence wrap after Z? No. chr continues to later code points.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Why start current at zero?
Pick an answer to check it.
Question 2
What happens for n = 0?
Pick an answer to check it.
Questions people ask
What happens for n = 0?
The pattern helper prints no rows.
Does the character sequence wrap after Z?
No. chr continues to later code points.
Why start current at zero?
chr(65 + 0) produces A.
๐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