0% found this document useful (0 votes)
5 views6 pages

Python Matrices

The document describes a console application called 'Matrix Mastery' that allows users to create and perform various operations on matrices, including addition, subtraction, multiplication, and transposition. It also features a random matrix generator and a practice quiz to test users' matrix operation skills. The application includes a menu for users to navigate through the different functionalities.

Uploaded by

Kashmala Durrani
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Python Matrices

The document describes a console application called 'Matrix Mastery' that allows users to create and perform various operations on matrices, including addition, subtraction, multiplication, and transposition. It also features a random matrix generator and a practice quiz to test users' matrix operation skills. The application includes a menu for users to navigate through the different functionalities.

Uploaded by

Kashmala Durrani
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

"""

Matrix Mastery (Console App)

Features
- Create matrices
- Matrix addition
- Matrix subtraction
- Matrix multiplication
- Matrix transpose
- Random matrix generator
- Practice quiz
"""

import random

# -----------------------------
# Matrix Creation
# -----------------------------

def create_matrix(rows, cols):

matrix = []

for i in range(rows):
row = []

for j in range(cols):

val = int(input(f"Enter value for [{i+1},{j+1}]: "))


[Link](val)

[Link](row)

return matrix

def print_matrix(matrix):

print()

for row in matrix:


print(row)

print()

# -----------------------------
# Matrix Operations
# -----------------------------

def add_matrices(A, B):

result = []

for i in range(len(A)):

row = []

for j in range(len(A[0])):

[Link](A[i][j] + B[i][j])

[Link](row)

return result

def subtract_matrices(A, B):

result = []

for i in range(len(A)):

row = []

for j in range(len(A[0])):

[Link](A[i][j] - B[i][j])

[Link](row)

return result

def multiply_matrices(A, B):

rows_A = len(A)
cols_A = len(A[0])
cols_B = len(B[0])

result = [[0 for _ in range(cols_B)] for _ in range(rows_A)]

for i in range(rows_A):

for j in range(cols_B):
for k in range(cols_A):

result[i][j] += A[i][k] * B[k][j]

return result

def transpose_matrix(A):

rows = len(A)
cols = len(A[0])

result = [[0 for _ in range(rows)] for _ in range(cols)]

for i in range(rows):
for j in range(cols):
result[j][i] = A[i][j]

return result

# -----------------------------
# Random Matrix Generator
# -----------------------------

def random_matrix(rows, cols):

matrix = []

for _ in range(rows):

row = []

for _ in range(cols):

[Link]([Link](1,9))

[Link](row)

return matrix

# -----------------------------
# Quiz System
# -----------------------------

def matrix_quiz():
score = 0
questions = 3

for i in range(questions):

A = random_matrix(2,2)
B = random_matrix(2,2)

print("\nMatrix A:")
print_matrix(A)

print("Matrix B:")
print_matrix(B)

op = [Link](["add","multiply"])

if op == "add":

correct = add_matrices(A,B)
print("Find A + B")

else:

correct = multiply_matrices(A,B)
print("Find A × B")

print("Enter result row by row")

user = []

for r in range(len(correct)):

row = list(map(int,input().split()))
[Link](row)

if user == correct:

print("Correct!")
score += 1

else:

print("Wrong.")
print("Correct answer:")
print_matrix(correct)

print("\nFinal Score:", score,"/",questions)


# -----------------------------
# Menu
# -----------------------------

def menu():

while True:

print("\nMatrix Trainer\n")
print("1. Add Matrices")
print("2. Subtract Matrices")
print("3. Multiply Matrices")
print("4. Transpose Matrix")
print("5. Matrix Quiz")
print("6. Exit\n")

choice = input("Choose: ")

if choice in ["1","2","3"]:

rows = int(input("Rows: "))


cols = int(input("Columns: "))

print("\nEnter Matrix A")


A = create_matrix(rows,cols)

print("\nEnter Matrix B")


B = create_matrix(rows,cols)

if choice == "1":

result = add_matrices(A,B)

elif choice == "2":

result = subtract_matrices(A,B)

else:

result = multiply_matrices(A,B)

print("\nResult:")
print_matrix(result)

elif choice == "4":


rows = int(input("Rows: "))
cols = int(input("Columns: "))

A = create_matrix(rows,cols)

result = transpose_matrix(A)

print("\nTranspose:")
print_matrix(result)

elif choice == "5":

matrix_quiz()

elif choice == "6":

print("Goodbye")
break

else:

print("Invalid choice")

# -----------------------------
# Main
# -----------------------------

if __name__ == "__main__":

menu()

You might also like