Question: Hello, I'm working on this python code and have gotten stuck. I'm trying to have the functions matrix_add, matrix_sub, matrix_multi, and matrix_element pull the variable

Hello, I'm working on this python code and have gotten stuck. I'm trying to have the functions "matrix_add", "matrix_sub", "matrix_multi", and "matrix_element" pull the variable from "matrix_enter", but it keeps looping back to the matrix input. Any help would be appreciated. Thank you. Python Code:

 import re import numpy as np print("***************** Welcome to the Python Matrix Application *****************") def main(): """main function to run matrix game""" while True: print("Do you want to play the Matrix Game?") select = input("Enter Y for yes or N for No: ") if select.lower() == "y": phone_num() zip_code() matrix_enter() matrix_menu() elif select.lower() == "n": print("***************** Thanks for playing Python Numpy *****************") break else: print("Please enter Y for yes or N for no.") def phone_num(): """function for phone number""" while True: phone = input("Enter your phone number (XXX-XXX-XXXX): ") if not re.match(r"\d{3}-\d{3}-\d{4}", phone): print("Your phone number is not in correct format. Please renter: ") else: break def zip_code(): """function for zipcode""" while True: zipcode = input("Enter your zipcode+4 (XXXXX-XXXX): ") if not re.match(r"\d{5}-\d{4}", zipcode): print("Your zipcode is not in correct format. Please renter: ") else: break def matrix_enter(): """function for the matrix menu""" print("Enter your first 3x3 matrix: ") first = [] for i in range(3): row = input().split() row = list(map(int, row)) first.append(row) print("Your first 3x3 matrix is: ") for i in range(3): for j in range(3): print(first[i][j], end="") print() print("Enter your second 3x3 matrix: ") second = [] for i in range(3): row = input().split() row = list(map(int, row)) second.append(row) print("Your second 3x3 matrix is: ") for i in range(3): for j in range(3): print(second[i][j], end="") print() return first, second def matrix_menu(): print("Select a Matrix Operation from the list below: ") print("A. Addition") print("B. Subtraction") print("C. Matrix Multiplication") print("D. Element by Element Multiplication") select2 = input() if select2.lower() == "a": matrix_add() if select2.lower() == "b": matrix_sub() if select2.lower() == "c": matrix_multi() if select2.lower() == "d": matrix_element() def matrix_add(): first, second = matrix_enter() print("You selected Addition. The results are: ") first = np.array(first) second = np.array(second) final = first + second for i in range(3): for j in range(3): print(final[i][j], end="") print() print("The Transpose is: ") tran = np.transpose(final) for i in range(3): for j in range(3): print(tran[i][j], end="") print() print("The row and column mean values of the results are: ") print("Row: ", np.mean(final, axis=1)) print("Column: ", np.mean(final, axis=0)) def matrix_sub(): first, second = matrix_enter() print("You selected Subtraction. The results are: ") first = np.array(first) second = np.array(second) final = first - second for i in range(3): for j in range(3): print(final[i][j], end="") print() print("The Transpose is: ") tran = np.transpose(final) for i in range(3): for j in range(3): print(tran[i][j], end="") print() print("The row and column mean values of the results are: ") print("Row: ", np.mean(final, axis=1)) print("Column: ", np.mean(final, axis=0)) def matrix_multi(): first, second = matrix_enter() print("You selected Matrix Multiplication. The results are: ") first = np.matrix(first) second = np.matrix(second) final = first * second final = np.array(final) for i in range(3): for j in range(3): print(final[i][j], end="") print() print("The Transpose is: ") tran = np.transpose(final) for i in range(3): for j in range(3): print(tran[i][j], end="") print() print("The row and column mean values of the results are: ") print("Row: ", np.mean(final, axis=1)) print("Column: ", np.mean(final, axis=0)) def matrix_element(): first, second = matrix_enter() print("You selected Element by Element Multiplication. The results are: ") first = np.array(first) second = np.array(second) final = first * second for i in range(3): for j in range(3): print(final[i][j], end="") print() print("The Transpose is: ") tran = np.transpose(final) for i in range(3): for j in range(3): print(tran[i][j], end="") print() print("The row and column mean values of the results are: ") print("Row: ", np.mean(final, axis=1)) print("Column: ", np.mean(final, axis=0)) main() 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!