how to solve error - list indices must be integers or slices, not float

def MinIndexDelta(delta):
    """
        Calculate min value in delta and return its index.
        param: list of delta values
        return: integer - index of min element in delta
    """
    min_index = 0
    for i in range(1,len(delta)):
        if(delta[i] == 0):
            continue
        elif(delta[i] < delta[min_index]):
            min_index = i
            
    return min_index
        

def MainRow(table, p0, m_column):
    """
        Calculate main row in current iteration
        param: list table - current values in table
        p0 condition vector
        m_column - main column
        return index of main row
    """
    
    #Each element is tuple values + index of row
    values = []
    
    #Calculate values for each row by formula
    for i in range(len(table)):
    
        #If devision by zero, ignore thi element
        if(table[i][m_column] == 0):
            continue
        elif(p0[i]/table[i][m_column] > 0):
            values.append((p0[i]/table[i][m_column], i))
    
    #Find index of min element in calculated list
    min_index = values[0][1]
    
    for i in range(1, len(values)):
        if(values[i][0] < values[min_index][0]):
            min_index = values[i][1]
            
    return min_index
        
def CheckDelta(delta):
    """
        If in delta is at least one < 0 return false, else rerturn true. Means end of simplex method
    """
    for i in delta:
        if i < 0:
            return False
    return True

def CalculateDelta(func_coef, cv, table):
    """
        Calculate delta vector by formula
    """
    return [sum([cv[i] * table[i][j] for i in range(len(table))]) - func_coef[j] for j in range(len(table[0]))]

def SimplexMethod(func_coef, p0, cv, table, basis):
    """
        Calculate result of simplex linear programming task.
        param: list func_coef - coeficients in function
        p0 vector of conditions
        cv vector of coeficients of basis from main function
        table - matrix of values in conditions expressions(before =)
        basis - value of x-s number(if basis contain x3, x4, x5, means basis = [3,4,5])
    """

    #Set current table
    current_table = table
    
    #Calculate delta
    delta = CalculateDelta(func_coef, cv, current_table)

    k = 1
    
    #Calculate till vector of delta has signed values or if k >= 10(Its posibly a loop)
    while(not CheckDelta(delta) and k < 10):
        
        #Set old table, which used to calculate new values
        old_table = current_table
        
        #Find main column and main row indexes
        m_column = MinIndexDelta(delta)
        
        m_row = MainRow(old_table, p0, m_column)
        
        #Calculate new basis, Cv and p0 vectors
        basis[m_row] = m_column + 1
        
        cv[m_row] = func_coef[m_column]
        p0[m_row] = p0[m_row]/current_table[m_row][m_column]
        
        #Calculate new table. First calculate main row by formula
        current_table = [old_table[m_row][i]/old_table[m_row][m_column] for i in range(len(table[0]))]
        #Then calculate others rows by formula
        current_table = [[old_table[i][j] - old_table[i][m_column] * current_table[j] if i != m_row else current_table[j] for j in range(len(old_table[0]))] for i in range(len(old_table))]
        
        #Update p0 vector
        p0 = [p0[j] - old_table[j][m_column] * p0[m_row] if j != m_row else p0[j] for j in range(len(p0))]
        
        #Update delta vector
        delta = CalculateDelta(func_coef, cv, current_table)
        
        k+=1
    
    #Calculate result. First, all elements are 0
    result = [0 for i in range(len(func_coef))]
    
    #Result has all x_i. If in basis vector are x_i set value from p0 as result.
    for i in range(len(p0)):
        result[basis[i] - 1] = p0[i]
    print("Simplex method comleted " + str(k) + " steps")
    return result



def MainInterface():
    """
        Its UI of application. No parameters and no return
    """
    
    print("Welcome")
    while(True):
        try:
            choose = int(input("1 - Your value\n2 - Example\n3 - Exit\n"))
        except ValueError:
            print("Invalid value, try again")
            continue
        if(choose == 1):
            
            try:
                
                rows = float(input("Enter rows in table: "))
                cols = float(input("Enter columns in table: "))
                func_coef = [float(input("Enter func_coef: ")) for i in range(int(cols))]
                cv = [float(input("Enter vector CV: ")) for i in range(int(rows))]
                p0 = [float(input("Enter vector p0: ")) for i in range(int(rows))]
                basis = [float(input("Enter basis: ")) for i in range(int(rows))]
                table = [[float(input("Enter table value: ")) for j in range(int(cols))] for i in range(int(rows))]
                
                res = SimplexMethod(func_coef, p0, cv, table, basis)
                print("Goal point is: " + str(res))
            
                print("Your goal is: " + str(sum([func_coef[i] * res[i] for i in range(len(res))])))
                
            except ValueError:
                print("Invalid value, try again")
                continue

        elif(choose == 2):
            #"""
            func_coef = [1, 1, 2, 1, 1]
            cv = [2, 1, 1]
            p0 = [4, 8, 6]
            basis = [3,4,5]
            table = [[1,2,1,0,0],[2,2,0,1,0],[3,1,0,0,1]]
            """
            func_coef = [-1,-2,1,-1]
            cv = [-1, 1]
            p0 = [4, 8]
            basis = [4,3]
            table = [[1,2,0,1],[1,1,1,0]]
            """
            
            res = SimplexMethod(func_coef, p0, cv, table, basis)
            print("Goal point is: " + str(res))
            
            print("Your goal is: " + str(sum([func_coef[i] * res[i] for i in range(len(res))])))
                
        elif(choose == 3):
            print("Goodbye")
            break
         
        else:
            print("Invaid value, try again")
            continue

MainInterface()

RESULT

File "C:/Users/User/Desktop/МО/9 лаба коди/6.py", line 182, in <module>
    MainInterface()

File "C:/Users/User/Desktop/МО/9 лаба коди/6.py", line 145, in MainInterface
res = SimplexMethod(func_coef, p0, cv, table, basis)

File "C:/Users/User/Desktop/МО/9 лаба коди/6.py", line 115, in SimplexMethod
result[basis[i] - 1] = p0[i]
TypeError: list indices must be integers or slices, not float

Ответы (1 шт):

Автор решения: passant

Ну вам же написали ответ ясно и однозначно, на том языке, на котором вы решили задавать вопрос на русскоязычном форуме, выполняя свою "МО/9 лаба коди".

TypeError: list indices must be integers or slices, not float

Даже указали и номер строчки и сам оператор

line 115, in SimplexMethod
result[basis[i] - 1] = p0[i]

Вот эту ошибку и исправляйте. Что не ясно?

→ Ссылка