James Jameson
James Jameson

Reputation: 57

How do I return a modified matrix from a function?

I need to create a function which takes a nested list of currencies + conversion rates as a parameter. Which then calculates how much $10, $20, $30, $40, $50, $60, $70, $80, $90 and $100 will buy of each currency.

So if the matrix passed to the function was:`

[['AUD', 0.96],['USD', 0.75],['Euro', 0.67],['GBP', 0.496]]

The output should look like this:

[[10, 9.6, 7.5, 6.7, 4.96], [20, 19.2, 15.0, 13.4, 9.92], [30, 28.799999999999997, 22.5, 20.1, 14.879999999999999], [40, 38.4, 30.0, 26.8, 19.84], [50, 48.0, 37.5, 33.5, 24.8], [60, 57.599999999999994, 45.0, 40.2, 29.759999999999998], [70, 67.2, 52.5, 46.900000000000006, 34.72], [80, 76.8, 60.0, 53.6, 39.68], [90, 86.39999999999999, 67.5, 60.300000000000004, 44.64], [100, 96.0, 75.0, 67.0, 49.6]]

I have come up with the following code:

def makeTable(lst):
res =[[]]
matrix = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
conversion = [item[1] for item in lst]

for num in m:
    res.append(num)
    for rate in convers:
        res.append(num * rate)
return (res)

result = makeTable([['AUD', 0.96], ['USD',0.75], ['Euro',0.67], ['GBP',0.496]])
print (result)

But it doesn't give me the output as a nested list which I am trying to get. Can anybody show me how to change my code so that the returned matrix is a list of sublists with each sublist containing the $ (10 - 100 in increments of 10) followed by the 4 different conversions.

Upvotes: 0

Views: 48

Answers (1)

Alchimie
Alchimie

Reputation: 426

Maybe this is helpful:

def makeTable(lst): 
    res =[] 
    matrix = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] 
    conversion = [item[1] for item in lst]

    for num in matrix: 
        m=[num,]

        for rate in conversion: 
            m.append(num * rate)
        res.append(m)
    return (res) 

result = makeTable([['AUD', 0.96], ['USD',0.75], ['Euro',0.67], ['GBP',0.496]]) 
print (result)

For limit float numbers at 3 numbers you can replace:

m.append(num * rate)

With:

m.append(round(num * rate,3))

Upvotes: 1

Related Questions