python list problems

Asked

Viewed 1,715 times

1

I’m trying to solve this problem in python:

Make a program that loads a list with the models of five cars (example of models: FUSCA, GOL, VECTRA etc). Upload another list with the consumption of these cars, that is, how many kilometers 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 this will cost, considering one that gasoline costs R $ 2,25 a liter.

Below follows an example screen. The layout of the information should be as close as possible to the example. The data is fictitious and can change with each execution 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 the peugeout.

Up here is how it should look. Follow my code below and what is missing to do. In this code I sent I’m just not sure how to find the lowest consumption value, 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))) 

2 answers

3

Depending on the data you present you can do the calculations all at once (in the same cycle), as well as you can popular the two lists also 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)

DEMONSTRATION

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

2

Another solution, simpler, but running away from what was explicitly requested by the statement, would be to create an object namedtuple to represent a particular car instead of storing the values in separate 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 on 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.