Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python function help
#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


Messages In This Thread
python function help - by hardz1n51 - Jun-08-2022, 09:54 PM
RE: python function help - by ndc85430 - Jun-09-2022, 04:56 AM
RE: python function help - by deanhystad - Jun-09-2022, 03:05 PM
RE: python function help - by rob101 - Jun-13-2022, 10:02 PM
RE: python function help - by ndc85430 - Jun-09-2022, 08:16 PM
RE: python function help - by deanhystad - Jun-09-2022, 08:25 PM
RE: python function help - by deanhystad - Jun-14-2022, 01:46 AM
RE: python function help - by rob101 - Jun-14-2022, 07:13 AM
RE: python function help - by buran - Jun-14-2022, 10:36 AM
RE: python function help - by rob101 - Jun-14-2022, 05:43 PM

Forum Jump:

User Panel Messages

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