How the value of a car depreciates.

Car value

Try

Watch this video to learn about the new concepts shown in the program:

Investigate

Questions to think about with this program to check your understanding:

Purpose question

Explain the purpose of the iteration and condition in line 9.

while value >= resale_value and resale_value > 0:
Reveal answer

The value of the car is recalculated until the value is less than the resale value and is also more than £0. The first condition stops when the resale value is reached and the second condition ensures the value cannot go below £0.

Reason question

Explain why line 13 is not indented.

Reveal answer

The final output statement about when to part exchange the car should not be inside the iteration because it needs to be executed after the year to part exchange has been calculated. This statement should only be output once, not after every calculation.

Make

Change the program so that it:

  1. Only allows the value of the car to be shown up to year 4. Cars cannot be part exchanged with this dealer after year 5.

Typical inputs and outputs from the program would be:

Enter the value of the car purchased: £8500
Enter the minimum part exchange value: £2000
In year 0 the car is worth £ 8500
In year 1 the car is worth £ 6375
In year 2 the car is worth £ 4781
In year 3 the car is worth £ 3585
In year 4 the car is worth £ 2688
Part exchange before the end of year 5
🆘 If you're really stuck, use this Parsons code sorting exercise
Complete program
# Car value program

# -------------------------
# Subprograms
# -------------------------
# Show the depreciation in the value of a car
def show_value(value, resale_value):
---
    year = 0
    depreciation = 0.25
---
    # Repeat until the resale value is reached is zero or five years data is shown
    while value >= resale_value and resale_value > 0 and year != 5:
---
        print("In year", year, "the car is worth £", value)
---
        value = int(value - (value * depreciation))
        year = year + 1
---
    print("Part exchange before the end of year", year)
---
    
    
# -------------------------
# Main program
# -------------------------
---
value = int(input("Enter the value of the car purchased: £"))
resale_value = int(input("Enter the minimum part exchange value: £"))
---
show_value(value, resale_value)