title: Please help me in solving Fractional Knapsack problem (Maximum Value of the Loot)
body: <h1>Maximum Value of the Loot</h1>
  <blockquote>
  <p><em><strong>Problem Introduction</strong></em>:<br/><br/>
  A thief finds much more loot than his bag can fit. Help him to find the most valuable combination of items assuming that any fraction of a loot item can be put into his bag.
  Problem Description<br/><br/>
  <strong>Task</strong>: The goal of this code problem is to implement an algorithm for the fractional knapsack problem. <br/>
  <strong>Input Format</strong>: The first line of the input contains the number  of items and the capacity  of a knapsack. The next  lines define the values and weights of the items. The -th line contains integers  and —the value and the weight of -th item, respectively. <br/>
  <strong>Constraints</strong>: 1≤≤103,0≤ ≤2·106;0≤ ≤2·106,0&lt; ≤2·106 for all 1≤≤.All the numbers are integers. <br>
  <strong>Output Format</strong> Output the maximal value of fractions of items that fit into the knapsack. The absolute value of the difference between the answer of your program and the optimal value should be at most
  −3
  your answer, while being computed correctly, can turn out to be wrong because of rounding issues).</p>
  </blockquote>
  <h2>Sample 1.</h2>
  <p>Input:
  <br>3 50
  <br>60 20
  <br>100 50
  <br>120 30
  <br><br>Output:
  <br>180.0000
  <br>To achieve the value 180, we take the first item and the third item into the bag.
  <br><br></p>
  <h2>Sample 2.</h2>
  <p>Input:
  <br>1 10
  <br>500 30
  <br>Output:
  <br>166.6667</p>
  <h1>My code:</h1>
  <pre><code># Maximum Value of the Loot
  
  def knapsack(n, capacity, value_list, weight_list):
      unitValues_list = []
      
      #First lets calculate the unitValues_list
      for i in range (n):
          unitValue = (value_list[i]/weight_list[i])
          unitValues_list.append(unitValue)
      
      #Now lets fill the knapsack, intake is how much is in the bag at the moment!
      intake = 0
      max_value = 0
      factor = True
  
      while(factor):
          max_index = unitValues_list.index(max(unitValues_list, default=0)) 
          # this gives the index of the max valued element
          
          if(weight_list[max_index] &lt;= capacity):
              # In this case, full item is taken in
              intake = weight_list[max_index]
              capacity -= weight_list[max_index]
              max_value += value_list[max_index]
              
          else:
              # weight_list[max_index] &gt; capacity
              # In this case, fraction to be taken
              intake += capacity
              capacity = 0
              max_value += unitValues_list[max_index]*intake
              
          weight_list.pop(max_index)
          value_list.pop(max_index)
          unitValues_list.pop(max_index)
          print(weight_list)
  
          n -= 1 #no. of items left
          factor = ((n != 0) if ((capacity != 0) if True else False) else False)
  
      return max_value
  
  if __name__ == &quot;__main__&quot;:
      value_list = []
      weight_list = []
      
      #The first line of the input contains the number  of items and the capacity  of a knapsack. 
      #The next  lines define the values and weights of the items. 
      
      n , capacity = map(int, input().split())
      
      for i in range (n):
          value , weight = map(int, input().split())
          value_list.append(value)
          weight_list.append(weight)
          
      #Output the maximal value of fractions of items that fit into the knapsack.
      print(&quot;{:.10f}&quot;.format(knapsack(n, capacity, value_list, weight_list)))
  </code></pre>
  <h2>I keep getting an error:<br></h2>
  <p>Failed case #6/13: Wrong answer<br>
  got: 8740.3008948546 expected: 7777.731<br>
  (Time used: 0.00/5.00, memory used: 9191424/671088640.)</p>