'Exercise in Exorcism: Exchange Rates

I have to create a script in Python that converts money while also adding in an exchange fee. Here are the exact instructions:

Create the exchangeable_value() function, taking budget, exchange_rate, spread, and denomination.

Parameter spread is the percentage taken as an exchange fee, written as an integer. It needs to be converted to decimal by dividing it by 100. If 1.00 EUR == 1.20 USD and the spread is 10, the actual exchange rate will be: 1.00 EUR == 1.32 USD because 10% of 1.20 is 0.12, and this additional fee is added to the exchange.

This function should return the maximum value of the new currency after calculating the exchange rate plus the spread. Remember that the currency denomination is a whole number and cannot be sub-divided.

Note: Returned value should be int type.

That said, here's the code I came up with:

def exchangeable_value(budget, exchange_rate, spread, denomination):
    new_exchange_rate = exchange_rate * ((spread/100)+1)
    x = budget/new_exchange_rate
    return x//denomination

For the sake of being thorough, here are the test inputs and the outputs I'm getting:

(100000, 10.61, 10, 1): 8568 (correct)

(1500, 0.84, 25, 40): 35.0, should be 1400

(470000, 1050, 30, 10000000000): 0 (correct)

(470000, 0.00000009, 30, 700): 5,738,705,738.0, should be 4,017,094,016,600

(425.33, 0.0009, 30, 700): 519.0, should be 363,300

FWIW, the answer is available to me, I just don't understand why my code isn't working.



Solution 1:[1]

The problem is with:

return x//denomination

You should not just divide the amount by the denomination, as that will return how many units you need of a denomination.

Instead you should return a multiple of denomination to cover for the desired value. So correct to:

return (x//denomination)*denomination

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 trincot