title: If statement issues in Python
body: <p>I am trying to use multiple <code>if</code> statements to check a certain condition and then to plot some data using <code>matplotlib</code> in <code>Python</code>. Firstly, I am doing <code>os.walk</code> on a directory to get the list of files and subsequently load them to finally plot and save the figures. Here is my code:</p>
  
  <pre><code>def velocity():
      plt.xlabel('$\mathrm{Iterations\/or\/Time}$')
      plt.title(r'$\mathrm{Residual\/history}$')
      plt.grid(True)
      plt.yscale('log')
  
      if 'Ux_0' in lf:
          print "Entered"
          plt.plot(time_x, value_x, color = 'r', label = 'x-vel')
      elif 'Uy_0' in lf:
          print "Entered"
          plt.plot(time_y, value_y, color = 'b', label = 'y-vel')
      elif 'Uz_0' in lf:
          print "Entered"
          plt.plot(time_z, value_z, color = 'g', label = 'z-vel')
  
          plt.legend()
          plt.savefig(os.path.join(plotsDir, 'velocity'), bbox_inches='tight', dpi=100)
          plt.close() 
  
      return (time_x, value_x, lf)
      return (time_y, value_y, lf)
      return (time_z, value_z, lf)
  
  for path, dirs, files in os.walk(logsDir, topdown=False):
      for lf in files:
          if 'Ux_0' in lf:
              logFile = os.path.join(path, lf)
              data_x = np.loadtxt(logFile)
              time_x, value_x = data_x[:,0], data_x[:,1]
              (time_x, value_x, lf) = velocity()
          if 'Uy_0' in lf:
              logFile = os.path.join(path, lf)
              data_y = np.loadtxt(logFile)
              time_y, value_y = data_y[:,0], data_y[:,1]
              (time_y, value_y, lf) = velocity()
          if 'Uz_0' in lf:
              logFile = os.path.join(path, lf)
              data_z = np.loadtxt(logFile)
              time_z, value_z = data_z[:,0], data_z[:,1]
              (time_z, value_z, lf) = velocity()
  </code></pre>
  
  <p>The <code>logDir</code> has only three files to begin with and they are <code>Ux_0</code>, <code>Uy_0</code> and <code>Uz_0</code>. Interestingly, after <code>os.walk</code> when I <code>print</code> <code>lf</code>, I get order of files as <code>Ux_0</code>, <code>Uz_0</code> and <code>Uy_0</code>. Now, the figure that is generated by the function <code>velocity()</code> has only data from <code>Ux_0</code> and <code>Uz_0</code> and not <code>Uy_0</code>. However, in my function if the order of <code>Uy_0</code> and <code>Uz_0</code> is reversed such that I have <code>Uz_0</code> immediately after <code>Ux_0</code> as shown below, I get all the three plots as desired.</p>
  
  <pre><code>if 'Ux_0' in lf:
      print "Entered"
      plt.plot(time_x, value_x, color = 'r', label = 'x-vel')
  elif 'Uz_0' in lf:
      print "Entered"
      plt.plot(time_z, value_z, color = 'b', label = 'z-vel')
  elif 'Uy_0' in lf:
      print "Entered"
      plt.plot(time_y, value_y, color = 'g', label = 'y-vel')
  
      plt.legend()
      plt.savefig(os.path.join(plotsDir, 'velocity'), bbox_inches='tight', dpi=100)
      plt.close() 
  
  return (time_x, value_x, lf)
  return (time_y, value_y, lf)
  return (time_z, value_z, lf)
  </code></pre>
  
  <p>I am not sure what is causing this.</p>