Python OOPE Exam
2024 Sep Oppe 1 Set 1
def print_n_pattern(n):
"""
Prints an "N" shaped pattern with n rows.
The pattern has vertical bars (|) on the left and right sides of each row,
with a backslash (\) moving diagonally from the top-left to the bottom-right.
There are no spaces to the right of the pattern.
Args:
n (int): The number of rows for the pattern (n > 0).
"""
for i in range(n):
# Start each row with a vertical bar
row = "|"
# Add leading spaces before the diagonal backslash
# The number of leading spaces is equal to the current row index 'i'
row += " " * i
# Add the diagonal backslash
row += "\\"
# Add trailing spaces after the diagonal backslash
# The total width of the inner part (between vertical bars) is (n - 1) characters.
# So, the number of trailing spaces is (n - 1) - i
row += " " * (n - 1 - i)
# End each row with a vertical bar
row += "|"
# Print the constructed row
print(row)
if __name__ == "__main__":
try:
# Read the integer input from the user
n = int(input())
# Validate the input: n must be greater than 0
if n <= 0:
print("Input 'n' must be a positive integer (n > 0).")
else:
# Call the function to print the pattern
print_n_pattern(n)
except ValueError:
# Handle cases where the input is not a valid integer
print("Invalid input. Please enter a single integer.")