Click here to Skip to main content
16,020,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I have this program which prompts the user for a matrix’ dimensions then for its elements.

I have done input validation correctly, but my problems is with asking to input at least the correct number of inputs needed to get the matrix
for example
Python
Please enter the matrix' number of rows: 2
Please enter the number of columns: 2. # number of elements = 4
Please enter the integer elements of the matrix in row-wise fashion, space- separated: 1 2 3
not enough elements. Again, please …
<Please enter the integer elements of the matrix in row-wise fashion, space- separated:1 2 3 4

output:
Python
1  2 
3  4


currently, I'm unable to get an output at all.

Also, if you know, I need it to print the type of matrix it is
for example:

The matrix is diagonal 
1  0  0 
0  2  0 
0  0  3


The matrix is upper triangular 
1  2  3
0  1  2
0  0  1


The matrix is symmetric 
1  2  3 
2  1  4 
3  4  1


The matrix is lower triangular 
1  0  0
2  1  0
3  2  1


What I have tried:

here is my code:
 <pre>print("This program finds out the properties of a given matrix")
while True:
    try:
        rowsNumber = int(input("Please enter the matrix' number of rows: "))
        if rowsNumber <= 0:
            print("The number of rows must be at least 1")
            continue
        else:
            break
    except ValueError:
        print("The number of rows must be a positive integer")
        continue

while True:
    try:
        columnsNumber = int(input("Please enter the number of columns: "))
        if columnsNumber <= 0:
            print("The number of columns must be at least 1")
            continue
        else:
            break
    except ValueError:
        print("The number of columns must be a positive integer")
        continue


matrix = []
count = 0
outputMatrixString = ""
entriesOfMatrix = input("Please enter the integer elements of the matrix in row-wise fashion, space- separated: ")
entriesOfMatrix = entriesOfMatrix.split()
numberOfInputs = rowsNumber * columnsNumber
for i in range(rowsNumber):
    for j in range(columnsNumber):
        for x in entriesOfMatrix:
            if x.isdigit():
                integerEntry = int(x)
                intgerToappend = integerEntry
                if intgerToappend < numberOfInputs:
                    print("not enough elements. Again, please ...")
                continue

            outputMatrixString += (str(intgerToappend)) + '\t'
        outputMatrixString+= '\n'
    break

print(outputMatrixString)
Posted
Updated 20-Mar-21 5:56am
v2

1 solution

Quote:
currently, I'm unable to get an output at all.

Your code is really weird, difficult to see what you try to do.
I would expect only 2 loops there:
Python
for i in range(rowsNumber):
    for j in range(columnsNumber):
        for x in entriesOfMatrix:


Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900