N
N
NotCoolProgrammer2021-12-23 08:08:57
Python
NotCoolProgrammer, 2021-12-23 08:08:57

TypeError: 'float' object is not iterable, how to fix?

Traceback error (most recent call last):
File "main.py", line 20, in
D[i] += math.fabs(math.fabs(A[i][j]) - (A[i][j ])**2)
TypeError: 'float' object is not iterable
Help me fix
Code:

import math

A = []
n = 8
m = 7
A = [[n + m for m in range(m)] for n in range(n)]
D = [[n] for n in range(n)]

print("Array A:")
for i in range(0, n):
  print()
  for j in range(0, m):
    A[i][j] = math.exp(i / (j + 1)) / (i + 1)
    print("{:6.6f}".format(A[i][j]), end = " ")
print("\n")

print("All the distances:")
for i in range(n):
  for j in range(m):
    D[i] += math.fabs(math.fabs(A[i][j]) - (A[i][j])**2)
  print("{:6.6f}".format(D[i]), end = " ")
print()

max = D[0]
maxI = 0
for i in range(n):
  if D[i] > max:
    max = D[i]
    maxI = i
print("The biggest distance from first row is:", "{:6.6f}".format(max))
print("Index of this distance is:", maxI)

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
D
desaki, 2021-12-23
@desaki

I don’t know if I think correctly, but why do you need += in the error line?
Is the following not correct?:
D[i] = math.fabs(math.fabs(A[i][j]) - (A[i][j])**2
This is what gave me:

spoiler
61c40e8fef94c173779623.png

G
galaxy, 2021-12-23
@galaxy

In D lies [[0], [1], [2], [3], [4], [5], [6], [7]]is a list of lists.
You are doing addition D[i] = D[i] + xwhere x is some real number. Those. add a number to the list.
Maybe you still wanted to initialize D differently?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question