Reputation: 1
I am creating sort of a compound interest calculator where and basically want python to create a neat list of every year. I don't have any code for this at the moment, but I will try to explain:
years = int(input('how many years?...')) # Lets say we use "3" as input
Then, I would like python to create a list containing as many elements that equals to the input from the variable (years). I understand that we have to create numbers in this case that range from 1 to x years.
Presented something like, and even better if the elements where split into individual lines:
list_years = [1 years of saving (x), 2 years of saving is (y), 3 year of saving is (z)]
I'm not even sure if this is the best way to accomplish what I want, but either way I am very interested in being able to create lists dynamically as a part of my learning process.
Update:
So I have found something that brings me a bit closer to the solution (I believe):
totalyears = int(input('how many years?'))
yearlist = [i for i in range(36)]
print(yearlist[1:totalyears +1])
Output: [1, 2, 3, 4, 5]
yearlist = [i for i in range(36)]
print(yearlist[1:totalyears +1])
#Output: [1, 2, 3, 4, 5]
Upvotes: 0
Views: 483
Reputation: 7892
Something along these lines does the job:
def savings_per_year(whichyear):
return whichyear * 2 # return with whatever function you choose
years = 3
list_years = [savings_per_year(eachyear) for eachyear in range(years)]
Then list_years
will be:
[0, 2, 4]
Recommend looking up list comprehension.
Upvotes: 1