Table in Python

Asked

Viewed 266 times

0

Good night,

I need to make a Python function that receives a list of tuples with real dollar and real dollar dollar quotations return a string that, when printed, generates a table of quotations.

I couldn’t generate the table. Can anyone help me? Thanks for the help.

  • Your program is not working because of the way you are accessing the elements inside your tuple. In the code print(f'US${lista[posição(0)]: <15}', end= '=') , posicao is a whole, which you are using to access the inside of lista the object in the "position" index which in your case is a tuple. Then the correct one would be lista[posição][0]. First you get the tuple and then you access its index.

1 answer

3


You can greatly simplify the code. For example, when dividing 1 / cotação_dólar, the result will already be a float (because the price is already one float), so you don’t have to force the result to float. And to build the list of exercise 7, you can use a range and eliminate some variables:

def exercício_7(cotação_dólar):
    lista = []
    for valor in range(1, 101):
        lista.append((valor, valor * cotação_dólar)) # dólar, real
        lista.append((valor, valor / cotação_dólar)) # real, dólar
    return lista

I used the range(1, 101) because the final value is not included (i.e., this range takes the numbers from 1 to 100).

Exercise 8 receives the list returned by exercise 7 (and not the quotation). And to print the way you need it, just test if the index is even or odd and treat the values accordingly:

def exercício_8(cotacoes): # recebe a lista de cotações
    for posição in range(len(cotacoes)):
        valor1, valor2 = cotacoes[posição] # valor1 e valor2 são os valores da tupla
        if posição % 2 == 0:
            currency1, currency2, end = 'US$', 'R$ ', '   '
        else:
            currency1, currency2, end = 'R$ ', 'US$', '\n'
        print(f'{currency1}{valor1:>8.2f} = {currency2}{valor2:>8.2f}', end=end)

        if end == '\n':
            print('-' * 25, ' ', '-' * 25)

That is, for each element of the list of quotations, I take the 2 values and see if they correspond to dollar or real (based on even or odd indices). I also see if it is to skip the line or print in it, and only put the strokes if you have skipped the line.

To format the numbers with 2 decimal places I used >8.2f (right align occupying 8 positions, and with 2 decimal places). Adjust the size for whatever you need, and read the documentation to see all available options.

To use the functions, just take the list returned by exercise 7 and move to 8:

cotação_dólar = float(input("Insira a cotação do dólar: "))
cotacoes = exercício_7(cotação_dólar)
exercício_8(cotacoes)

Or simply:

exercício_8(exercício_7(float(input("Insira a cotação do dólar: "))))

The output will be something like this (I used the quotation equal to 5 in this example):

US$    1.00 = R$     5.00   R$     1.00 = US$    0.20
-------------------------   -------------------------
US$    2.00 = R$    10.00   R$     2.00 = US$    0.40
-------------------------   -------------------------
US$    3.00 = R$    15.00   R$     3.00 = US$    0.60
-------------------------   -------------------------
etc...

Another option to iterate from 2 to 2 is using iter, creating an iterator from the list, and zip, that runs through several iterables at the same time. So I can scroll through the same iterator, and the result will be a loop going through the list of 2 out of 2:

def exercício_8(cotacoes):
    iterador = iter(cotacoes)
    currency1, currency2 = 'US$', 'R$ '
    for (v1, v2), (v3, v4) in zip(iterador, iterador):
        print (f'{currency1}{v1:>8.2f} = {currency2}{v2:>8.2f}   {currency2}{v3:>8.2f} = {currency1}{v4:>8.2f}')
        print('-' * 25, ' ', '-' * 25)

And if you want to make the size configurable:

def exercício_8(cotacoes):
    tamanho = 8 # mude o tamanho que cada número vai ocupar
    # calcula o restante com base no tamanho
    formato = f'>{tamanho}.2f'
    hífens = '-' * (tamanho * 2 + 9)
    footer = f'{hífens}   {hífens}'

    iterador = iter(cotacoes)
    currency1, currency2 = 'US$', 'R$ '
    for (v1, v2), (v3, v4) in zip(iterador, iterador):
        print (f'{currency1}{v1:{formato}} = {currency2}{v2:{formato}}   {currency2}{v3:{formato}} = {currency1}{v4:{formato}}')
        print(footer)

Browser other questions tagged

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