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:

*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:

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