~1 min read • Updated Oct 11, 2025
Program Overview
This Python program reads an even number from the user and prints the character 'B' in each line using a loop.
If the input number is odd, the program displays an error message and exits.
This exercise helps reinforce basic loop structures and conditional checks in Python.
Python Code:
def print_b_rows(n):
if n % 2 != 0:
print("Please enter an even number.")
return
for _ in range(n):
print("B")
# Run the program
rows = int(input("Enter an even number of rows: "))
print_b_rows(rows)
Sample Output (input: 6):
B
B
B
B
B
B
Step-by-Step Explanation:
- First, the program checks whether the input number is even
- If it is, a for loop prints the character 'B' in each row
- If the number is odd, an error message is shown and the loop is skipped
Written & researched by Dr. Shahin Siami