Skip to content
Advertisement

python: codingbat no_teen_sum – why my function isn’t working as expected?

Below is the code I used for the no_teen_sum and subsequent fixed_teen functions.

The first code is what I submitted – and worked for all test cases:

JavaScript

And the fix_teen function that is called:

JavaScript

However, looking at this I saw a lot of repitition and realized maybe I had misread what the question was asking. It was valid in terms of finding a solution but not as clean as it could be. So I tried to work on an improvement.

Improved code:

JavaScript

And the modified fix_teen function:

JavaScript

The issue I am having for example a test case of (1, 2, 18) is that it returns 21. It should return 3. I tried putting print statements in between each ‘fix_teen’ call in the main function to see what value it had for a, b, c and it just left them as is (1, 2, 18) rather than (1, 2, 0)

The weird part is if I called fixed_teen(18) independently it returns 0.

Advertisement

Answer

Your no_teen_sum(a, b, c) function is returning a + b + c (which is literally what gets passed to the function)! You should make a, b and c equal to the result from the fix_teen function to get the desired result!

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement