Powering the DeLorean

You're the proud new owner of a DeLorean sports car. DeLoreans are best known for being able to travel through time. You can't wait to try it out, you've already started listing all the different times you are going to visit. You have written down the year of each destination in a file called years.txt.

However, every time traveller knows that you need plutonium to generate the 1.21 gigawatts of electricity needed. You will need 0.1 grams of plutonium for each year that you travel through. For example, travelling from 2000 to 2011 will take 1.1 grams.

Write a program that will work out the total amount of plutonium that you will need to visit each year in years.txt in order, starting in the year 2011. If years.txt contains:

1955
2015
1885

then your program should print out

24.6 grams of plutonium are needed.

Thats the problem

This is my code

myfile=open("years.txt", "rU")
(listo) =["2011"]
n = 0
t = 0
for line in myfile:
    listo.append(line.rstrip())

for i in listo:
    a1 = listo[i]
    a2 = listo[i+1]
    t = t + (0.1 * (int(a1) - int(a2)))

print t

It isnt working! Someone please help me out!

I think the example result is wrong as you do not want to stay at year 1885 with 0 gram plutonium, but to come home without jury rigging a steam train!

Your problem is that you take year from listo, but instead of using it and keeping track which you are (say current = 2011 , before the loop, updating it inside the loop, listo = [] at line 2), you try to use it as index to list of years.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.