..

Applying Knowledge - Class 8

Create a modular Python program that uses lists to determine the most fuel-efficient car model.

The program should consist of the following functions:

  • input_car(): Prompts the user to input four car models from the keyboard.
  • input_consumption(): Allows the user to enter the fuel consumption for each car model in liters per kilometer.
  • efficient(): Returns the model of the most fuel-efficient car. Note that the car model and its consumption should be stored in the same list but in separate arrays (one for cars and one for consumption).

Data input should be conducted in the following order:

  • The user types in the model of each car, one per line, in sequence.
  • Then, the user enters the consumption data for each of the four cars, one per line.

The program will then display the model of the most fuel-efficient car, which is the one with the lowest fuel consumption value.

def main():
    input_car()
    input_consumption()
    print(efficient())
main()

Input:

  1. Four car models, each entered on a new line.
  2. Fuel consumption for each of the four cars, entered on a new line.

Output:

  • The model of the most fuel-efficient car.

Sample Inputs/Outputs:

foo@bar:~$ py baz.py
JEEP
AUDI
BMW
JAGUAR
10
6
8
12
AUDI
foo@bar:~$

SOLUTION:

def entrada_carro():
    carros = []
    for _ in range(4):
        c = input()
        carros.append(c)
    return carros

def entrada_consumo():
    consumos = []
    for _ in range(4):
        i = int(input())
        consumos.append(i)
    return consumos

def economico(modelos, consumos):
    indice = consumos.index(min(consumos))
    return modelos[indice]

def main():
    modelos = entrada_carro()
    consumos = entrada_consumo()
    modelo_economico = economico(modelos, consumos)
    print(modelo_economico)

main()