Read and Write Matrix in Console in Python
def main():
"""
Reads a matrix from the user and prints it out.
"""
# Get the dimensions of the matrix
row_count = int(input("Enter the number of rows: "))
col_count = int(input("Enter the number of cols: "))
# Read the matrix from the user
print("Enter the matrix:")
matrix = read_matrix(row_count, col_count)
# Print the matrix
print("The matrix is:")
print_matrix(matrix)
def read_matrix(row_count: int, col_count: int):
"""
Reads a matrix from the console.
"""
matrix: list[list[int]] = []
for _ in range(row_count):
row = list(map(int, input().split()[:col_count]))
matrix.append(row)
return matrix
def print_matrix(matrix: list[list[int]]):
"""
Prints a matrix to the console.
"""
for row in matrix:
for element in row:
print(element, end=" ")
print()
if __name__ == "__main__":
main()