problems list in python

0

I'm trying to solve this problem in python:

Make a program that loads a list with the five car models (example of models: FUSCA, GOL, VECTRA etc). Charge another list with the consumption of these cars, that is, how many miles each of these cars makes with a liter of fuel.

Calculate and show:

  • The most economical car model;
  • How many liters of fuel each of the registered cars consumes to cover a distance of 1000 kilometers and how much it will cost, considering that the gasoline costs R $ 2.25 a liter.

Below is a sample screen. The provision of information should be as close as possible to the example. The data is fictitious and may change with each run of the program.

Comparison of Fuel Consumption

Veículo 1
Nome: fusca
Km por litro: 7
Veículo 2
Nome: gol
Km por litro: 10
Veículo 3
Nome: uno
Km por litro: 12.5
Veículo 4
Nome: Vectra
Km por litro: 9
Veículo 5
Nome: Peugeout
Km por litro: 14.5

Relatório Final
 1 - fusca           -    7.0 -  142.9 litros - R$ 321.43
 2 - gol             -   10.0 -  100.0 litros - R$ 225.00
 3 - uno             -   12.5 -   80.0 litros - R$ 180.00
 4 - vectra          -    9.0 -  111.1 litros - R$ 250.00
 5 - peugeout        -   14.5 -   69.0 litros - R$ 155.17

The lowest consumption is from peugeout.

Up here is how it should stay. Here's my code below and what's missing to do. In this code I sent only I'm not sure how to find the lowest value of the consumption, to print the car. If anyone can help me thank you.

My code:

listaCarro = [None] * 5
listaConsumo = [None] * 5
listaDistancia = [None] * 5
listaCusto = [None] * 5

precoGasolina = 2.25
distancia = 1000

for i in range(0,5):
    print ('Digite o nome {} carro: '.format(i+1))
    listaCarro[i] = input()

for x in range(0,5):
    print('Digite o consumo {} carro (km por litro): '.format(x+1))
    listaConsumo[x] = float(input())
    listaDistancia[x] = distancia / listaConsumo[x]
    listaCusto[x] = listaDistancia[x] * precoGasolina     

for j in range(0,5):
    print('Veiculo {}'.format(j+1))
    print('Nome: {}'.format(listaCarro[j]))
    print('Km por litro: {}'.format(listaConsumo[j]))

for p in range(0,5):
    print('{} - {}    -    {}  -  {} litros - R$ {}\n'.format(p+1, listaCarro[p], listaConsumo[p], round(listaDistancia[p],1), round(listaCusto[p],2))) 
    
asked by anonymous 11.01.2018 / 11:19

2 answers

3

Depending on the data that you present you can do the calculations all at once (in the same cycle), just as you can also populate the two lists in the same cycle:

listaCarro = []
listaConsumo = []

while len(listaCarro) < 5:
    listaCarro.append(input('Digite o nome do carro: '))
    listaConsumo.append(float(input('Digite o consumo do carro (km por litro): ')))
    print('novos dados inseridos\n')

results = ''
valor_gas = 2.25
total_km = 1000
for j, c in enumerate(listaCarro):
    print('Veiculo {}'.format(j+1))
    print('Nome: {}'.format(c))
    print('Km por litro: {}\n'.format(listaConsumo[j]))

    consumo_l = round(total_km/listaConsumo[j], 2)
    results += 'O carro {} consume {}L e custará $R{} quando fizer {}km\n'.format(c, consumo_l, round(consumo_l*valor_gas, 2), total_km)

print('O carro mais económico é o {}'.format(listaCarro[listaConsumo.index(max(listaConsumo))])) # descobrir na listaCarro o carro cujo o indice e o mesmo do que o indice do maior valor na listaConsumo
print(results)

STATEMENT

NOTE: The car that makes more km per liter is the most economical

    
11.01.2018 / 12:38
1

Another solution, simpler, but avoiding what was explicitly requested by the statement, would be to create a namedtuple object to represent a particular car instead of storing the values in different lists.

from collections import namedtuple
from operator import attrgetter

Carro = namedtuple('Carro', ('nome', 'rendimento'))

carros = [
  Carro(nome='Fusca', rendimento=7.0),
  Carro(nome='Gol', rendimento=10.0),
  Carro(nome='Uno', rendimento=12.5),
  Carro(nome='Vectra', rendimento=9.0),
  Carro(nome='Peugeout', rendimento=14.5)
]

carro_mais_economico = max(carros, key=attrgetter('rendimento'))

print('Mais econômico:', carro_mais_economico.nome)

for i, carro in enumerate(carros):
  consumo = 1000 / carro.rendimento
  gasto = 2.25 * consumo
  print(f'{i+1} - {carro.nome:<10} - {carro.rendimento:>5} - {consumo: >6.2f} - R$ {gasto:.2f}')

See working at Repl.it

The result would be:

Mais econômico: Peugeout
1 - Fusca      -   7.0 - 142.86 - R$ 321.43
2 - Gol        -  10.0 - 100.00 - R$ 225.00
3 - Uno        -  12.5 -  80.00 - R$ 180.00
4 - Vectra     -   9.0 - 111.11 - R$ 250.00
5 - Peugeout   -  14.5 -  68.97 - R$ 155.17
    
12.04.2018 / 18:28