papara
papara

Reputation: 25

How can I properly loop this multiplication?

I'm trying to calculate the depreciation of an object over 20 years by 15% of its original value each year. The problem is it only subtracts the result of the first multiplication into the rest of the loop instead of decreasing the value by 15% 20 times.

ovalue = int(input("Enter the value of the car: "))
res = ovalue * 0.15
fvalue = ovalue - res
year = 1
while (year <= 20):
   print("Year ",year,":", fvalue)
   year = year+1
   fvalue = fvalue-res

Is there any way to fix this? Thank you so much

Upvotes: 0

Views: 73

Answers (3)

Ranga
Ranga

Reputation: 701

As many have said, you want to multiply by the depreciation value. Note that if it depreciates 15%, it's new value will be 85% of its current value. Since this is multiplication we can use powers.

def depreciate(initialValue, depreciationRate, numYears):
    depreciationRate = 1 - depreciationRate
    return initialValue * (depreciationRate ** numYears)

Upvotes: 0

hamid.khb
hamid.khb

Reputation: 452

price = []
for i in range(1,20):
    value -= value*0.15
    price.append(value)

Upvotes: 1

I. Yegor
I. Yegor

Reputation: 366

This will decrease the value by 15% of current value on each iteration

value = int(input("Enter the value of the car: "))

year = 1
while year <= 20:
   value -= value * 0.15
   print("Year ",year,":", value)
   year = year+1

This will decrease the value by 15% of original value on each iteration (which is probably not what you want)

value = int(input("Enter the value of the car: "))

deduction = value * 0.15
year = 1
while year <= 20:
   value -= deduction
   print("Year ",year,":", value)
   year = year+1

Upvotes: 2

Related Questions