How the value of a car depreciates.
Select the button below to open the Python program in a new window. Run the program and read the lines of code to see if you can understand how it works. It will be helpful to arrange your display so that you can have this browser window on one side of the screen and the code on the other.
Watch this video to learn about the new concepts shown in the program:
Questions to think about with this program to check your understanding:
Explain the purpose of the iteration and condition `while value >= resale_value and resale_value > 0:` in line 9.
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.
Explain why line 13 is not indented.
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.
Change the program so that it:
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
Use these resources to help you meet the success criteria if you need support or syntax reminders.
while
Can also be used with mathematical operators and more than one condition.
The following operators can be used in conditions. They all return True or False:
==
Is equal to. E.g. ` while x == y` means repeat the indented code until x does not equal y.
!=
is not equal to. E.g. `while x != y` means repeat the indented code until x is the same as y.
<
Is less than. E.g. `while x < y` means repeat the indented code until x is not less than y.
>
Is greater than. E.g. `while x > y` means repeat the indented code until x is not greater than y.
<=
Is less than or equal to. E.g. `while x <= y` means repeat the indented code until x is not less than or equal to y.
>=
Is greater than or equal to. E.g. `while x >= y` means repeat the indented code until x is not greater than or equal to y.
The following Boolean operators can be used to join multiple conditions into a single selection statement that returns either True or False:
and
Both conditions must be True to continue the iteration.
or
One of the conditions must be True to continue the iteration.
not
The condition must be False to continue the iteration.
You can have as many conditions in one statement as you need. Use brackets () for order of prescedence.
Run the unit tests below to check that your program has met the success criteria.
Enter the value of the car purchased: £20000
Enter the minimum part exchange value: £5000
In year 0 the car is worth £ 20000
In year 1 the car is worth £ 15000
In year 2 the car is worth £ 11250
In year 3 the car is worth £ 8437
In year 4 the car is worth £ 6327
Part exchange before the end of year 5
Enter the value of the car purchased: £12000
Enter the minimum part exchange value: £8500
In year 0 the car is worth £ 12000
In year 1 the car is worth £ 9000
Part exchange before the end of year 2
Enter the value of the car purchased: £20000
Enter the minimum part exchange value: £1000
In year 0 the car is worth £ 20000
In year 1 the car is worth £ 15000
In year 2 the car is worth £ 11250
In year 3 the car is worth £ 8437
In year 4 the car is worth £ 6327
Part exchange before the end of year 5
Enter the value of the car purchased: £35000
Enter the minimum part exchange value: £5000
In year 0 the car is worth £ 35000
In year 1 the car is worth £ 26250
In year 2 the car is worth £ 19687
In year 3 the car is worth £ 14765
In year 4 the car is worth £ 11073
Part exchange before the end of year 5