-2
To generate this two-column matrix is very simple, just use a loop for
to go through each percentage obtained and thus, perform its calculation adding to the matrix a list containing the value obtained and the result of its calculation.
See the code below:
def gera_matriz(*valores):
matriz = []
for valor in valores:
linha = [valor, calcular(valor)]
matriz.append(linha)
return matriz
def calcular(porcentagem):
# Edite essa função para alterar o cálculo
return porcentagem * 3.42
I did not understand very well the part of the calculation because by multiplying the values by 342
you will not get the results that appear in the table shown. So if you want, just change the function calcular()
to correct the calculation of percentages.
Bonus, below is a simple code to print that formatted table:
def formatar(numero):
numero = "%.2f" % numero
numero = numero.replace(".", ",")
return numero
matriz = gera_matriz(0.0, 0.28, 0.56, 0.83)
print("----- ----")
for linha in matriz:
print(formatar(linha[0]) + "%" + " " + formatar(linha[1]))
Above I wrote a very simple code since you seem to be beginner in the Python language, but if you want to decrease a few more lines and make the code more beautiful, you can use comprehensilist on to do the same as before, only better.
See below how it would look:
def gera_matriz(*valores):
return [[valor, calcular(valor)] for valor in valores]
Much smaller and more beautiful than before is not even ? :)
See online the code I made: https://repl.it/repls/FluffySaddlebrownCleaninstall
Apart from the rounding problems (which you can choose how to treat better), it’s more or less like this: https://ideone.com/GTstjT
– hkotsubo