Reputation: 1
I have exam in python about some weeks and our professor gave us some tasks to prepare for the exam. One of the tasks I found very difficult and I thought I solved it, but seems like I used the wrong code. I checked youtube for similar codes where I can use the tasks, but couldn't fully understand it. I just wonder how you guys would've solve this. Until then, I'll try to find a solution for this. The code I used is below the task. The task itself is below;
Calculator for calculating used car value
The drop in value is a big cost in keeping a car. The drop in value is usually greatest in the first year, and then decreases throughout the car's lifetime. The decline in value is calculated based on the following rates:
depreciation in the first year: 20% of the new car price
depreciation second year: 14% of the used price (ie of the used value after the first year)
depreciation third year: 13% of the used price (ie of the used value after the second year)
fourth year depreciation: 12% of the used price (etc., see above...)
depreciation fifth year: 11% of the used price
depreciation sixth year: 10% of the used price
Based on the new car price and the number of years the car is, the calculator must calculate the loss in value in NOK and the residual value/use value of the car. The calculator must have a printout of the purchase price, the car's age, loss in value in NOK and the used value of the car.
The program must be able to run as long as the user wishes.
I gave up after I found out I did it all wrong. I was watching a youtube video while doing this, and half way in I stopped because it didn't make sense. I think the task is much more simpler then that. I think you're supposed to use the if, else statement.
The code I used to try to solve the task:
# importing section
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import scipy as sp
# using the Csv file
df = pd.read_csv('output.csv')
# Checking the first 5 entries of dataset
df.head()
headers = ["symboling", "normalized-losses", "make",
"fuel-type", "aspiration", "num-of-doors",
"body-style", "drive-wheels", "engine-location",
"wheel-base", "length", "width", "height", "curb-weight",
"engine-type", "num-of-cylinders", "engine-size",
"fuel-system", "bore", "stroke", "compression-ratio",
"horsepower", "peak-rpm", "city-mpg", "highway-mpg", "price"]
df.columns = headers
df.head()
data = df
# Finding the missing values
data.isna().any()
# Finding if missing values
data.isnull().any()
# converting mpg to L / 100km
data['city-mpg'] = 235 / df['city-mpg']
data.rename(columns={'city_mpg': "city-L / 100km"}, inplace=True)
print(data.columns)
# checking the data type of each column
data.dtypes
I tried to go on youtube and solve the task by finding similar codes there. I need actually help with this cause I have no one to guide me.
Upvotes: 0
Views: 94
Reputation:
you can do a for loop that iterates over years and updates the price
value = 1000.0
depreciation_list = [0.2, 0.14, 0.13, 0.12, 0.11, 0.10]
print(f"Value new: {value}")
for year, depreciation in enumerate(depreciation_list):
new_value = value - value * depreciation
print(f"Value after year {year+1}: {new_value} ({round(depreciation*100)}% of {value})")
value = new_value
Result:
Value new: 1000.0
Value after year 1: 800.0 (20% of 1000.0)
Value after year 2: 688.0 (14% of 800.0)
Value after year 3: 598.56 (13% of 688.0)
Value after year 4: 526.7328 (12% of 598.56)
Value after year 5: 468.792192 (11% of 526.7328)
Value after year 6: 421.9129728 (10% of 468.792192)
Now you just need to incorporate this into your data
Upvotes: 1