How do I declare a line in a text file as a variable?
<p>For example, I have opened a text file and found a product that the user wants to "buy". The products are listed on a notepad text file, with the product name then a new line then the cost of the product, for example</p>

<pre><code>radiators
0.50
fridge
0.50
</code></pre>

<p>This is what I have done so far:</p>

<pre><code>product = input("What product would you like?")
userfile = open ("products.txt","r")
    lines = userfile.readlines()
    for i in range(0, len(lines)):
        line = lines[i]
        if product in (line):
            found = True
            print("Found " + line)
            print("This product is " + lines[i+1])
            print("This product costs " +lines[i+2])
</code></pre>

<p>I need to declare <code>lines[i+2]</code> as a variable so I can multiply it like an integer. Is there a way that I can do this?</p>

<p>Try this out :</p>

<p>product = input("What product would you like?")</p>

<pre><code>userfile = open ("products.txt","r")
lines = userfile.readlines()

for line in lines:
   line = line.rstrip('\n')
   if product in line:
        found = True
        print("Found " + line)
        print("This product is " + lines[i+1])
        print("This product costs " + int(lines[i+2]))
</code></pre>

python file lines readlines