Python Forum

Full Version: python function help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
could someone help me with this one?

Define a function with the name “subtractElements”, which receives as parameters a list of integers (listNums) and whose body presents a NON-recursive code that subtracts all elements from this list and presents the result on the screen. To test pass a list with the values ​​[100, 20, 4, 10], the expected output is 66. (Copy your code here) :
What have you tried and where are you stuck? We will help, but won't do the work for you.
From your example, result = 100 - 20 - 4 - 10

This could also be represented as result = 100 - (20 + 4 + 10)

In both cases the first value is treated one way, and all subsequent values are treated in a different way.

Here's a start:
def subtractElements(listNums):
    result = ???
    print("The result is", result

subtractElements([100, 20, 4, 10])
Personally, I prefer to get the computer to do the work of checking the results. So, I write tests:

import unittest


class SubtractElementsTest(unittest.TestCase):
    def test_it_subtracts_all_the_elements_from_the_list(self):
        result = subtractElements([100, 20, 4, 10])

        self.assertEqual(result, 66)


if __name__ == '__main__':
    unittest.main()
Saving this in a file called test_subtract_elements.py and running it gives

Output:
$ python3 test_subtract_elements.py E ====================================================================== ERROR: test_it_subtracts_all_the_elements_from_the_list (__main__.SubtractElementsTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "test_subtract_elements.py", line 6, in test_it_subtracts_all_the_elements_from_the_list result = subtractElements([100, 20, 4, 10]) NameError: name 'subtractElements' is not defined ---------------------------------------------------------------------- Ran 1 test in 0.000s FAILED (errors=1)
So now I can gradually make it work, using the tests for feedback.
My guess is this is one of those classes where you need to print the result because the class has a script that runs the programs, captures stdout and grades the result.
(Jun-09-2022, 03:05 PM)deanhystad Wrote: [ -> ]From your example, result = 100 - 20 - 4 - 10

This could also be represented as result = 100 - (20 + 4 + 10)

In both cases the first value is treated one way, and all subsequent values are treated in a different way.

Here's a start:
def subtractElements(listNums):
    result = ???
    print("The result is", result

subtractElements([100, 20, 4, 10])

I hope I'm not contravening any kind of protocol with this post; please tell me if I am so that I don't make the same mistake again.

That aside, I've come up with this as a solution.


def subtractElements(listNums):
    result = listNums[0]
    for number in range(1, len(listNums)):
        result -= listNums[number] 
    print(result)

subtractElements([100, 20, 4, 10])
... but having done that, I read this: https://python-forum.io/thread-362.html

So, how to apply what is being said in that 'common pitfalls and what to do' to what I've done?

I ask simply because I want to improve my Python skills.

With thanks.
I would use sum().
(Jun-14-2022, 01:46 AM)deanhystad Wrote: [ -> ]I would use sum().

Thanks.

I'm aware of and have used the sum() function in one way or another, but I've never even considered it for use when subtracting numbers (maybe I'm missing a trick). I'll have to think about that one.
(Jun-14-2022, 07:13 AM)rob101 Wrote: [ -> ]I'll have to think about that one.
Hint: think slices
(Jun-14-2022, 10:36 AM)buran Wrote: [ -> ]
(Jun-14-2022, 07:13 AM)rob101 Wrote: [ -> ]I'll have to think about that one.
Hint: think slices

Ah... I think I see what you're driving at:
So, I'd leave 'result' as is, then slice 'listNums' from 1 to the end within the sum() function, which can then be decremented from 'result', right?

n.b: I'd post the one line of code that demonstrates the above, but I've already messed up and don't want to do that again.