doubts of logic in Python

Asked

Viewed 1,653 times

2

I am a beginner in the world of programming and also in python.

Solving some exercises I came across this

Make a Program for a paint shop. The program should ask for the size in square meters of the area to be painted. Consider that the paint cover is 1 liter for every 6 square meters and that the paint is sold in 18 liter cans, which cost R $ 80,00 or 3,6 liter gallons, which cost R $ 25,00. Inform the user the quantities of ink to be purchased and the respective prices in 3 situations: buy only 18-litre cans; buy only 3.6 litre gallons; mix cans and gallons so that the waste of paint is less. Add 10% off and always round up the values, that is, consider full cans.

My resolution works, I arrive at the expected result, but the logic I used is very extensive. how could it resolve this issue more efficiently? follows below the code. Thanks in advance.

import math

m2 = float(input('informe a quantidade de METRO QUADRADO (m²) a ser pintado: '))
consumo_litro = m2 / 6
print('\no consumo de tinta é: {:.2f} LITROS\n'.format(consumo_litro))
qtd_latas18 = consumo_litro / 18
qtd_latas36 = consumo_litro / 3.6
print('a quantidade de GALOES de 18 LITROS a ser usado é: {}'.format(math.ceil(qtd_latas18)))
print('a quantidade de LATAS de 3,6 LITROS a ser usado é: {}\n'.format(math.ceil(qtd_latas36)))
valor_total18 = math.ceil(qtd_latas18) * 80
valor_total36 = math.ceil(qtd_latas36) * 25
print('o valor total em GALOES de 18 LITROS é: R${}'.format(valor_total18))
print('o valor total em LATAS de 3,6 LITROS é: R$ {}\n'.format(valor_total36))
print('considerando o menor desperdíciode tinta, temos: \n')

qtd_latas_mistas18 = ((consumo_litro * 0.10) + consumo_litro) / 18
qtd_litros18 = math.trunc(qtd_latas_mistas18) * 18
resto18 = ((consumo_litro * 0.10) + consumo_litro) - qtd_litros18
qtd_latas_mistas36 = resto18 / 3.6
qtd_latas_mistas_total = math.trunc(qtd_latas_mistas18) + math.ceil(qtd_latas_mistas36)
valor_misto18 = math.trunc(qtd_latas_mistas18) * 80
valor_misto36 = math.ceil(qtd_latas_mistas36) * 25
total_misto = valor_misto18 + valor_misto36

print('quantidade de latas de 18 litros: {}'.format(math.trunc(qtd_latas_mistas18)))
print('qtd latas de 3,6: {}'.format(math.ceil(qtd_latas_mistas36)))
print('qtd latas mistas: {}'.format(qtd_latas_mistas_total))
print('\no valor total considerando GALOES e LATAS é (acresentando 10% de quebra): R$ {}'.format(total_misto))
  • One mistake I noticed is that you consider the 10% off only for the calculation of mixed cans when you should consider for all situations. To facilitate consider the area to be painted plus 10%. An operator that would facilitate its calculation in the mixed option is the rest of the division (%). It could eliminate some intermediate variables but with the time of programming practice will identify these situations.

  • your code is not bad, it is well written the names of the variables, but the good is you do not put all it, since it is work of the college, comes another and copies your work and still runs the risk of your teacher find your code here.

  • Another thing: A tin can has 18 l and the gallon 3.6 l. In printing you write backwards.

  • thanks for all your help. @Marco Souza is actually not going to college. I decided to learn programming and am performing some exercises of the internet

  • For the sake of organization, it would be good to separate the code into functions, simplifying the parts, this will also help to reuse some of these parts

1 answer

0


First thing that pops out the eyes is the amount of magic numbers. I’ve turned everything into constants (which are actually not constants, they’re just variables with high-box names). This increases code readability and facilitates future changes.

Note that the safety margin is 1.1, nay 0.1, 'cause it’ll make it a little easier.

M2_POR_LITRO = 6
PRECO_GALAO = 80
LITROS_GALAO = 18
PRECO_LATA = 25
LITROS_LATA = 3.6
MARGEM_SEGURANCA = 1.1

After that, I take the user input exactly as you did, but I already add the safety margin right there, because in my interpretation of the problem statement this margin should be applied to all situations, not just the third.

m2 = float(input('informe a quantidade de METRO QUADRADO (m²) a ser pintado: '))

consumo_litro = m2 / M2_POR_LITRO * MARGEM_SEGURANCA

Moving on, it’s time to process the data.

I decided to do the math before printing the results. This does not change the output of the pro user program at all, but leaves the code separated into "logical blocks": first the input, then the processing, then the output. I have the opinion that this makes the flow of code clearer.

Note that I simplified the calculation of mixed inks, I took advantage of the operator //, sometimes called integer Division.

qtd_galao_apenas = math.ceil(consumo_litro / LITROS_GALAO)
valor_galao_apenas = qtd_galao_apenas * PRECO_GALAO

qtd_lata_apenas = math.ceil(consumo_litro / LITROS_LATA)
valor_lata_apenas = qtd_lata_apenas * PRECO_LATA

qtd_galao_misto = consumo_litro // LITROS_GALAO
qtd_lata_misto = math.ceil((consumo_litro - qtd_galao_misto * LITROS_GALAO) / LITROS_LATA)
valor_galao_misto = qtd_galao_misto * PRECO_GALAO
valor_lata_misto = qtd_lata_misto * PRECO_LATA

To finish, I printed all the results on the screen.

The biggest difference to your code is that I used the f-strings instead of the method format.

Another difference is that I used several commands print() to generate blank line instead of \n inside the strings. I feel this makes the code easier to understand, as it looks a lot like output in practice. This is purely a matter of taste and you can keep putting the \n to generate blank lines without any problem.

print()
print(f'o consumo de tinta é: {consumo_litro:.2f} LITROS')
print()
print(f'a quantidade de GALOES de 18 LITROS a ser usado é: {qtd_galao_apenas:.0f}')
print(f'o valor total em GALOES de 18 LITROS é: R$ {valor_galao_apenas:.2f}')
print()
print(f'a quantidade de LATAS de 3,6 LITROS a ser usado é: {qtd_lata_apenas:.0f}')
print(f'o valor total em LATAS de 3,6 LITROS é: R$ {valor_lata_apenas:.2f}')
print()
print('considerando o menor desperdíciode tinta, temos:')
print(f'quantidade galões: {qtd_galao_misto:.0f}')
print(f'quantidade latas: {qtd_lata_misto:.0f}')
print(f'quantidade total mistas: {qtd_galao_misto + qtd_lata_misto:.0f}')
print(f'valor total considerando GALOES e LATAS é: R$ {valor_galao_misto + valor_lata_misto:.2f}')

The next step would be to break the program into several functions, and I encourage you to do so from now on, especially in longer programs.

  • 1

    Gabriel, thank you very much for the explanation. I see that I am on the right track, but I still have a lot ahead of me! ’s good to see other ways to solve the same problem, as I have to gain more experience with other resolutions, the question. Helped a lot. Thank you.

Browser other questions tagged

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