-2

I'm doing a matrix with python exporting at excel (library openpyxl) with a request by keyboard cell by cell. My question is how in that request I don't request the diagonal of the matrix and instead refill it with zeros.

Here is my code:

#LIBRERIAS
import openpyxl as opxl
import sys
import numpy as np

cant_nodos = 3

#FUNCIONES

def min_nodos(): #this function request the number of nodes.
    cant_nodos = int(input("Ingresar cantidad de nodos a utilizar (mínimo 6)"))
    while(cant_nodos < 6):
        print("ERROR: Elija mínimo 6 nodos y que sea entero positivo. ")
        cant_nodos = int(input("Ingresar cantidad de nodos a utilizar (mínimo 6)"))
    return cant_nodos

    
def crear_matriz_adyacente(cant_nodos): #this function create the matrix in excel with the nodes.
    libro = opxl.Workbook()
    pagina = libro.active
    pagina.title = "matriz_de_adyacencia"
    lista_nodos = []
    lista_valores = []
    cont = 0

    while(cont < cant_nodos):
        contador = str(cont+1)
        nodo = str(input("Ingrese nodo " +contador+ ":"))
        if nodo not in lista_nodos:
            lista_nodos.append(nodo)
            pagina.cell(row = 1, column = cont+2, value = nodo)
            pagina.cell(row = cont+2, column = 1, value = nodo)
            cont = cont+1
        else:
            print("ERROR: Nodo existente, escoja otro: ")
        

    **for fila in range(len(lista_nodos)):
        for columna in range(len(lista_nodos)):
            valor = int(input("Ingrese valor de nodo " +lista_nodos[fila]+" con el nodo " +lista_nodos[columna]+ ":"))
        
            while(valor < 0):
                print("ERROR: Valor negativo. Ingrese un valor positivo")
                valor = int(input("Ingrese valor de nodo " +lista_nodos[fila]+" con el nodo " +lista_nodos[columna]+ "(si el valor pertenece a la diagonal (i=j) ingresar un 0):"))
        
            pagina.cell(row = fila+2, column = columna+2, value = valor)**
                
    print(lista_valores)
        
    libro.save("matriz_adyacente.xlsx")

    return crear_menu()



def abrir_matriz_adyacente(matriz_adyacente): #this function open the matrix in python to use it because I need it for some algorithms (kruskal and dijkstra)
    excel = opxl.load_workbook(matriz_adyacente)
    lista_excel = []
    pagina = excel.active
    maximo_columna = pagina.max_column
    maximo_fila = pagina.max_row

    for fila in range(1, maximo_fila+1):
        lista_fila = []
        for columna in range(1, maximo_columna+1):
            espacio = pagina.cell(row = fila, column = columna)
            lista_fila.append(espacio.value)
        lista_excel.append(lista_fila)
    
    return lista_excel
    
crear_menu()
crear_matriz_adyacente(cant_nodos)
excel = abrir_matriz_adyacente("matriz_adyacente.xlsx")

With this I'm doing that the user input a number of nodes (in this case I limit it to 3), and then he refill the matrix in excel cell by cell with some value. But, they don't have to refill the diagonal of the matrix because it has to be zeros. I don't know how to do it, i had been trying to do it a lot, but i couldn't find some answer by myself, i would appreciate a lot your help.

Thank you!.

toborrm
  • 35
  • 6

1 Answers1

1

it would have been helpful to specify which portion of the code you want to edit, especially when things are named in a different language. From what I understand, all you need to do is this:

for fila in range(len(lista_nodos)):
    for columna in range(len(lista_nodos)):
        if fila == columna:
            valor = 0
        else:
            valor = int(input("Ingrese valor de nodo " +lista_nodos[fila]+" con el nodo " +lista_nodos[columna]+ ":"))
    
        while(valor < 0):
            print("ERROR: Valor negativo. Ingrese un valor positivo")
            valor = int(input("Ingrese valor de nodo " +lista_nodos[fila]+" con el nodo " +lista_nodos[columna]+ "(si el valor pertenece a la diagonal (i=j) ingresar un 0):"))
    
        pagina.cell(row = fila+2, column = columna+2, value = valor)
            
elbashmubarmeg
  • 330
  • 1
  • 9
  • Thank you a lot. Yes, it was that portion. I'm sorry, I'm kinda new in this forum, I'll do it for the next time. I had try that solution before, but it gave me an error. I might have miss something – toborrm Apr 28 '22 at 04:51