#Python > Currency exchange > Task 6: I am not understanding the last request of this requirement.

39 messages ยท Page 1 of 1 (latest)

flint adder
#

I think I understand the first part of what the function should do in terms of increasing the exchange rate so that it factors-in the cost of exchanging with that booth / currency provider.

But, I am not understanding the last question the way it is written in Task 6:

a) It seems that the exercise is requesting to calculate the "new currency" amount resulting from multiplying the budget by the increased (by cost) exchange rate.

b) But, then why do we need the "denomination" parameter in the function?
We can calculate the new currency amount regardless of the denomination, right?

I am not understanding how the denomination impacts the Max-Value of the resulting new currency value.

Could someone explain?

Thank you very much.

This is my unfinished code for Task 6:

    """
    :return: int - maximum value you can get.
    """
    actual_spread = spread / 100
    total_exchange_rate = exchange_rate * (1 + actual_spread)
    maximum_value = ???
    return maximum_value
wintry parrot
#

Similar to prior functions, this function is supposed to return the amount you get when you're exchanging currently and receiving your money in only one denomination.

reef ivyBOT
flint adder
#

Hello @wintry parrot , thank you for your input. I have evolved my code in the last function exchangeablevalue() as follow: ```def exchangeable_value(budget, exchange_rate, spread, denomination):
# Convert spread percentage to decimal
actual_spread = spread / 100

# Calculate total exchange rate including spread
total_exchange_rate = exchange_rate * (1 + spread_decimal)

# Calculate total value in the new currency
total_value_in_new_currency = budget * total_exchange_rate

# Calculate number of whole units of new currency
whole_units = int(total_value_in_new_currency // denomination)

# Return maximum value after exchange
return whole_units * denomination```` but I am still getting an error. The root cause of my difficulty with this Task 6 is my misunderstanding of the final request.
wintry parrot
#

That looks roughly correct. Does it pass the tests?

flint adder
#

nop, it does not pass the test. I am working on it but it's not passing it yet.

wintry parrot
#

If you'd like help debugging what's going wrong, it's helpful to see the code and the test details ๐Ÿ™‚

#

Could you share the code and the test failures for that code?

flint adder
#

yes, let me copy past the whole code and the one of the tests' results

#

I have to copy past the code in a file because it is too long for discord, give me a min

wintry parrot
#

You can split it across posts. Though if only the last function fails, you shouldn't need to share the other functions.

flint adder
#

right, ok, so here is the last function which fails (all prior functions work out well): ```def exchangeable_value(budget, exchange_rate, spread, denomination):
# Convert spread percentage to decimal
actual_spread = spread / 100

# Calculate total exchange rate including spread
total_exchange_rate = exchange_rate * (1 + spread_decimal)

# Calculate total value in the new currency
total_value_in_new_currency = budget * total_exchange_rate

# Calculate number of whole units of new currency
whole_units = int(total_value_in_new_currency // denomination)

# Return maximum value after exchange
return whole_units * denomination````
#

And here comes the test result:

#
test_data = [(100000, 10.61, 10, 1),
             (1500, 0.84, 25, 40),
             (470000, 1050, 30, 10000000000),
             (470000, 0.00000009, 30, 700),
             (425.33, 0.0009, 30, 700)]

result_data = [8568, 1400, 0, 4017094016600, 363300]

for variant, (params, expected) in enumerate(zip(test_data, result_data), start=1):
    budget, exchange_rate, spread, denomination = params

    with self.subTest(f"variation #{variant}",
                      budget=budget,
                      exchange_rate=exchange_rate,
                      spread=spread,
                      denomination=denomination,
                      expected=expected):

        actual_result = exchangeable_value(budget, exchange_rate, spread, denomination)
        error_message = (f'Called exchangeable_value{budget, exchange_rate, spread, denomination}. '
                         f'The function returned {actual_result}, but '
                         f'The tests expected {expected} as the maximum '
                         f'value of the new currency .')

        self.assertEqual(actual_result, expected, msg=error_message)
TEST FAILURE
One or more variations of this test failed. Details can be found under each [variant#].````
#

I am going to share another test below

wintry parrot
#

One or more variations of this test failed. Details can be found under each [variant#].
You should look at a varient ๐Ÿ˜‰

flint adder
#

Here is a "Variant", I have spotted an issue: ```.CODE RUN
test_data = [(100000, 10.61, 10, 1),
(1500, 0.84, 25, 40),
(470000, 1050, 30, 10000000000),
(470000, 0.00000009, 30, 700),
(425.33, 0.0009, 30, 700)]

result_data = [8568, 1400, 0, 4017094016600, 363300]

for variant, (params, expected) in enumerate(zip(test_data, result_data), start=1):
budget, exchange_rate, spread, denomination = params

with self.subTest(f"variation #{variant}",
                  budget=budget,
                  exchange_rate=exchange_rate,
                  spread=spread,
                  denomination=denomination,
                  expected=expected):

    actual_result = exchangeable_value(budget, exchange_rate, spread, denomination)
    error_message = (f'Called exchangeable_value{budget, exchange_rate, spread, denomination}. '
                     f'The function returned {actual_result}, but '
                     f'The tests expected {expected} as the maximum '
                     f'value of the new currency .')

    self.assertEqual(actual_result, expected, msg=error_message)

TEST FAILURE
NameError: name 'spread_decimal' is not defined

#

====> 'spread_decimal' is not defined

#

let me correct that

#

I have corrected the variable name spread_decima where it is declared. I am getting another error so will now share the code and test/variant again:

#

My updated code is: ```def exchangeable_value(budget, exchange_rate, spread, denomination):
# Convert spread percentage to decimal
actual_spread = spread / 100

# Calculate total exchange rate including spread
total_exchange_rate = exchange_rate * (1 + actual_spread)

# Calculate total value in the new currency
total_value_in_new_currency = budget * total_exchange_rate

# Calculate number of whole units of new currency
whole_units = int(total_value_in_new_currency // denomination)

# Return maximum value after exchange
return whole_units * denomination````

and the test result/variant is comming up now below:

#
test_data = [(100000, 10.61, 10, 1),
             (1500, 0.84, 25, 40),
             (470000, 1050, 30, 10000000000),
             (470000, 0.00000009, 30, 700),
             (425.33, 0.0009, 30, 700)]

result_data = [8568, 1400, 0, 4017094016600, 363300]

for variant, (params, expected) in enumerate(zip(test_data, result_data), start=1):
    budget, exchange_rate, spread, denomination = params

    with self.subTest(f"variation #{variant}",
                      budget=budget,
                      exchange_rate=exchange_rate,
                      spread=spread,
                      denomination=denomination,
                      expected=expected):

        actual_result = exchangeable_value(budget, exchange_rate, spread, denomination)
        error_message = (f'Called exchangeable_value{budget, exchange_rate, spread, denomination}. '
                         f'The function returned {actual_result}, but '
                         f'The tests expected {expected} as the maximum '
                         f'value of the new currency .')

        self.assertEqual(actual_result, expected, msg=error_message)
TEST FAILURE
AssertionError: 1167100 != 8568 : Called exchangeable_value(100000, 10.61, 10, 1). The function returned 1167100, but The tests expected 8568 as the maximum value of the new currency .````
#

1167100 != 8568. ===> something is totally wrong in the result I am getting.

wintry parrot
#

Compare how you calculate total_value_in_new_currency to your def exchange_money(). Do those look similar?

flint adder
#

where es def exchange_money() ?

#

I do not know where are you seeing the function exchange_money(), to compare it with my total_value_in_new_currency , where are you seeing it?

wintry parrot
#

It ought to be in your code somewhere. One of the earlier tasks asks you to write that function.

flint adder
#

This is my code for you to have it at hand: ```def exchangeable_value(budget, exchange_rate, spread, denomination):
# Convert spread percentage to decimal
actual_spread = spread / 100

# Calculate total exchange rate including spread
total_exchange_rate = exchange_rate * (1 + actual_spread)

# Calculate total value in the new currency
total_value_in_new_currency = budget * total_exchange_rate

# Calculate number of whole units of new currency
whole_units = int(total_value_in_new_currency // denomination)

# Return maximum value after exchange
return whole_units * denomination````
wintry parrot
#

Your solution has other functions, right?

flint adder
#

yes

wintry parrot
#

Is one of those named exchange_money()?

flint adder
#

oh, yeah, this is it (the very first function of the exercise): ```def exchange_money(budget, exchange_rate):
"""
USD divided by the exchange rate
"""
return budget / exchange_rate````

wintry parrot
#

Does that look vaguely similar-ish to this line?

    total_value_in_new_currency = budget * total_exchange_rate
flint adder
#

OK, I have to divide instead of multiplying, right? (on Task 6), let me try it out (thank you @wintry parrot )

wintry parrot
#

You're welcome ๐Ÿ™‚

flint adder
#

It is working fine now ๐Ÿ˜‰

wintry parrot
#

You could also call that function instead of re-implementing that logic!

flint adder
#

absolutely, good point, let me do it to try if it works

wintry parrot
#

(That's the sort of input you typically get from a code review/mentoring session)