Exercise
Screen Buffer Using 2D Array
Objective
Develop a Python program that declares a 70x20 two-dimensional array of characters, "draws" 80 letters (X, for example) in random positions, and displays the content of the array on screen.
Example Python Exercise
Show Python Code
import random
# Define the dimensions of the array
rows = 20
cols = 70
# Create a 70x20 two-dimensional array initialized with spaces
array = [[' ' for _ in range(cols)] for _ in range(rows)]
# Draw 80 'X' characters in random positions
for _ in range(80):
row = random.randint(0, rows - 1)
col = random.randint(0, cols - 1)
array[row][col] = 'X'
# Display the content of the array
for row in array:
print(''.join(row))
Output
X
X X
X X
X X X
X X
X
X X
X
X X X
X X X
X X X
X X X X
X X X
Share this Python Exercise