python function help - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: python function help (/thread-37424.html) |
python function help - hardz1n51 - Jun-08-2022 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) : RE: python function help - ndc85430 - Jun-09-2022 What have you tried and where are you stuck? We will help, but won't do the work for you. RE: python function help - deanhystad - Jun-09-2022 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]) RE: python function help - ndc85430 - Jun-09-2022 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 So now I can gradually make it work, using the tests for feedback.
RE: python function help - deanhystad - Jun-09-2022 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. RE: python function help - rob101 - Jun-13-2022 (Jun-09-2022, 03:05 PM)deanhystad Wrote: From your example, result = 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. RE: python function help - deanhystad - Jun-14-2022 I would use sum(). RE: python function help - rob101 - Jun-14-2022 (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. RE: python function help - buran - Jun-14-2022 (Jun-14-2022, 07:13 AM)rob101 Wrote: I'll have to think about that one.Hint: think slices RE: python function help - rob101 - Jun-14-2022 (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. |