In 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 reaches 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:
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:
currentFuelLevel = RANDOM NUMBER BETWEEN 1 AND 55
OUTPUT "You have " + currentFuelLevel + " litres left in your tank."
tankCapcacity = 55
unleadedPrice = 1.17
superUnleadedPrice = 1.27
dieselPrice = 1.21
dieselPremiumPrice = 1.34
OUTPUT " ---- Our Tariffs ----"
OUTPUT "A - Unleaded: £" + unleadedPrice + " per litre"
OUTPUT "B - Super Unleaded: £" + superUnleadedPrice + " per litre"
OUTPUT "C - Diesel: £" + dieselPrice + " per litre"
OUTPUT "D - Diesel Premium: £" + dieselPremiumPrice + " per litre"
fuelChoice = INPUT("What fuel do you need?")
pricePerLitre = 0
IF fuelChoice == "A" THEN
pricePerLitre = unleadedPrice
ELSEIF fuelChoice == "B" THEN
pricePerLitre = superUnleadedPrice
ELSEIF fuelChoice == "C" THEN
pricePerLitre = dieselPrice
ELSEIF fuelChoice == "D" THEN
pricePerLitre = dieselPremiumPrice
ELSE
OUTPUT "Invalid Option"
END IF
fullTank = INPUT "Would you like to fill up to a full tank?"
IF fullTank == "Yes" THEN
quantityNeeded = tankCapacity – currentFuelLevel
ELSE
quantityNeeded = INPUT("How many litres do you need?")
IF (currentFuelLevel + quantityNeeded) > tankCapacity THEN
quantityNeeded = tankCapacity – currentFuelLevel
END IF
END IF
cost = quantityNeeded * pricePerLitre
OUTPUT "Fuel Choice: " + fuelChoice
OUTPUT "Quantity: " + quantityNeeded + " litres."
OUTPUT "Total Cost: £" + cost
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
fullTank = INPUT "Would you like to fill up to a full tank?" WHILE fullTank NOT IN ["Yes", "No"] OUTPUT "Invalid Answer. Please answer the question with Yes or No" fullTank = INPUT "Would you like to fill up to a full tank?" END WHILE


Solution...
The solution for this challenge is available to full members!Find out how to become a member:
➤ Members' Area
An ATM, a.k.a. Cash Withdrawal Machine, uses a computer program to interact with the customer and count the number of banknotes to dispense based on the customer’s requested amount.
In this challenge we have created a client-side script using HTML, CSS and JavaScript to help a Maths teacher create homework worksheets for their class.
Prolog is a language built around the Logical Paradigm: a declarative approach to problem-solving.

Othello (a.k.a. Reversi) is a strategy board game for two players, played on an 8×8 board. There are sixty-four identical game pieces called discs which are white on one side and black on the other. Players take turns placing discs on the board with their assigned color facing up. During a play, any discs of the opponent’s color that are in a straight line and bounded by the disc just placed and another disk of the current player’s color are turned over to the current player’s color.
Moroccan mosaic, aka Zellige (الزليج), is a form of Islamic art and one of the main characteristics of Moroccan architecture. It consists of geometrically patterned mosaics, used to ornament walls, ceilings, fountains, floors, pools and tables. Each mosaic is a tilework made from individually chiseled geometric tiles set into a plaster base.




In this challenge we are going to create a cat’s age convertor find out how old a cat is in “human years”. This is very useful to understand more about cats and the care they need and to find out at what stage of life a cat is.

