0

I have an excerpt of code:

      while True:

           if a == float(b[0]):
               y = b[1]
               x.append(y)

           else: 
               a = a + 1

Where b is a list two columns wide. The problem is that the if statement does not iterate across all of the elements, and therefore the else statement is never reached. I was wondering how I could fix this?

1 Answers1

0
for item in b:
   if a == float(item[0]):
      y = item[1]
      x.append(y)
   else:
      a = a + 1

Also, you should be aware that comparing two float numbers using == may cause unexpected behaviors:

  2.2 * 3.0 == 6.6
  False
  3.3 * 2.0 == 6.6
  True

See topics like What is the best way to compare floats for almost-equality in Python?

Community
  • 1
  • 1
Green Su
  • 2,318
  • 2
  • 22
  • 16