def print_pattern():
for i in range(5, 0, -1):
for j in range(i, 0, -1):
print(chr(64 + j), end="")
print()
print_pattern()
Solution and Explanation:
def print_pattern()::
This line defines a function named print_pattern().
for i in range(5, 0, -1)::
This outer loop iterates over the range from 5 down to 1. The third argument -1 in range() means that it decrements by 1 in each iteration.
for j in range(i, 0, -1)::
This inner loop iterates over the range from the current value of i down to 1. So, for each iteration of the outer loop, this inner loop prints characters in decreasing order.
print(chr(64 + j), end=""):
Inside the inner loop, chr(64 + j) converts the integer ASCII value 64 + j to the corresponding character. Since we're starting from 'E' (ASCII value 69) and decrementing, 64 + j gives the ASCII value of the characters 'A' to 'E'.
end="" parameter is used to prevent print() from adding a newline character after each print, so the characters are printed horizontally.
print():
This print() statement is outside the inner loop. It's used to move to the next line after each inner loop iteration, creating a new line for the next set of characters.
print_pattern():
This line outside the function definition calls the print_pattern() function, causing the pattern to be printed according to the code within the function.
0 Comments:
Post a Comment