Automatic Petrol Pump Algorithm

petrol-pumpIn this challenge we will implement an algorithm to be used in an Automatic Petrol Pump to interact with the customer and calculate the total cost of filling up their vehicle.

The fuel options and tariffs at this petrol station are as follows:

  • Unleaded: £1.17 per litre,
  • Super Unleaded: £1.27 per litre,
  • Diesel: £1.21 per litre,
  • Diesel Premium: £1.34 per litre,

The petrol pump will interact with the customer by asking the following questions:

  • What fuel do you require?
  • Do you want to fill up (full tank capacity) and if not how many litres of fuel do you require?

The petrol pump will use this information provided by the customer as well as the tariffs provided above to calculate the total cost of this order.

To simulate a real life situation, we will assume that the customer’s vehicle has a tank capacity of 55 litres.
We will also assume that when the customer reach the petrol station, the vehicle still has some fuel left. To simulate this we will generate a random number between 0 and 55. This number will represent the number of litres left in the tank before filling up.

If the customer requires a full tank, the algorithm will calculate the quantity needed as follows:

Quantity Needed = Full Tank Capacity – Current Fuel Level

If the customer specifies a desired quantity (in litres), the algorithm will fill up the tank with the required quantity. However, if the required quantity combined with the current level of fuel in the tank exceeds the full tank capacity, the required quantity will be replaced to just the quantity that is needed using the same formula as mentioned above.

Petrol Pump Algorithm: Pseudocode


The suggested pseudocode for our Automatic Petrol Pump is as follows:

Your task is to use this pseudocode to implement this algorithm using Python.

Extension Task


Add some input validation to ensure that, if the user does not provide a valid answer, the same question is being asked again.

You should use:

  • A Lookup Check to check that the user is answering either A, B, C or D when being asked about their fuel choice?
  • A Lookup Check to check that the user is answering either Yes or No to the question: Would you like to fill up to a full tank?
  • A Type Check to ensure that the user is entering a valid positive number when asked about the number of litres they do require.

Pseudocode for Lookup validation

petrol-station

Share Button
Posted in Computer Science, Python - Intermediate, Python Challenges Tagged with: ,

Our Latest Book

Follow this blog