In: Computer Science
Write a program to ask three options to the user and continue until user selects option 3.
The first two options prompt a user to enter a string of numbers separated by spaces which is a 2by2 matrix. Do addition and subtraction on the input matrices when user selects 1 and 2.
You must write functions for matrix addition, subtraction and print. You may use the following function str_to_mlist to convert string to a list.
code example is given:
def str_to_mlist(m_str):
"""Converts a string separated by spaces to a list
Parameters
----------
m_str : str, required
Input string separated by spaces
Returns
-------
list
Numbers
"""
return m_str.split()
A sample run of the code looks like this:
Enter an option
1.Add two matrices
2.Subtract two matrices
3.Quit
1
Enter four numbers separated by spaces and press enter:3 4 -10
3
3 4
-10 3
Enter four numbers separated by spaces and press enter:8 9 0
-2
8 9
0 -2
Your result is:
11 13
-10 1
Enter an option
1.Add two matrices
2.Subtract two matrices
3.Quit
2
Enter four numbers separated by spaces and press enter:1 0 0
1
1 0
0 1
Enter four numbers separated by spaces and press enter:0 -1 -1
0
0 -1
-1 0
Your result is:
1 1
1 1
Enter an option
1.Add two matrices
2.Subtract two matrices
3.Quit
3