Screen Buffer Using 2D Array - Python Programming Exercise

In this exercise, you will 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. This exercise is perfect for practicing array manipulation and randomization in Python. By implementing a program that randomly places characters in a two-dimensional array and displays the result, you will gain hands-on experience in handling arrays and random functions in Python. This exercise not only reinforces your understanding of arrays and randomization but also helps you develop efficient coding practices for managing and displaying data.

 Category

Arrays, Lists, and Strings

 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

 Copy 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

 More Python Programming Exercises of Arrays, Lists, and Strings

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  2D Array: Display Circle

    In this exercise, you will develop a Python program that creates a 70x20 two-dimensional array of characters, "draws" a circle with a radius of 8 inside it, and displ...

  •  Software Applications

    In this exercise, you will develop a Python program that can store up to 1,000 records of software applications. For each application, you must keep the following dat...

  •  Exercise tasks

    In this exercise, you will develop a Python program that can store up to 2,000 "to-do tasks". For each task, it must keep the following data: Date (a set of 3 ...

  •  Home Finances

    In this exercise, you will develop a Python program that can store up to 10,000 records of expenses and incomes to create a small household accounting system. For eac...

  •  Array Reversal

    This Python program prompts the user to input 5 numbers and stores them in an array (or list in Python). After all the numbers are entered, the program displays them ...

  •  Array Lookup

    his Python program asks the user to input a specific number of floating-point numbers and stores them in a list. The program then repeatedly prompts the user to enter...