Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python function help
#1
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) :
Reply
#2
What have you tried and where are you stuck? We will help, but won't do the work for you.
Reply
#3
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])
Reply
#4
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.
Reply
#5
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.
Reply
#6
(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.
buran write Jun-14-2022, 05:37 AM:
Please, don't provide complete solution, even a poor one, to no effort questions. That's even more important in Homework section
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#7
I would use sum().
Reply
#8
(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.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#9
(Jun-14-2022, 07:13 AM)rob101 Wrote: I'll have to think about that one.
Hint: think slices
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
(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.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020