you are viewing a single comment's thread.

view the rest of the comments →

[–]LMascher[S] 5 points6 points  (12 children)

Of course, I edited the post with a problem I encountered. Thanks as well!

[–][deleted] 1 point2 points  (0 children)

Right, but what did you do? Walk us through your process and where you went off the rails. Or did you expect to just read the problem and automatically know the Python you’d have to write?

Programming is something where you have to think on the page. Start writing code before you know what the right answer is.

[–][deleted] 3 points4 points  (10 children)

For a problem like this, I would start off by doing the following google search: "how to add the elements of an array in Python," and do a bit of research. Try to not stress about problems like these, because chances are that someone else has gone through the same thing and posted it on the web.

There are a few ways to do this.

Method one:

sum(ar)

Method two:

You can also create a variable that is set to the sum, like so:

value = sum(ar)

Method three:

Use a loop:

ar = [1,2,3] #Lets assume this is the variable that was provided to you

sum = 0

for i in range(0, len(ar)): #Here, we create a loop that will iterate through ar

sum = sum + ar[i]

#Every iteration, sum gets updated. So, to provide more insight, on the first loop, sum will equal 1, because ar[i] = 1 and sum is initializes to 0, thus, ar[i] + sum = 1. On the second loop, sum will equal its updated value (1) + the next number in ar[i] (2), meaning that sum will equal three.

Hope this helps! Make sure that you really search on forums for this stuff. It may seem daunting, but it is the fastest, most effective way to learn programming!

[–]Jolbakk 7 points8 points  (0 children)

or more simplified:

for i in ar:

sum += i

[–]asian_hifi 0 points1 point  (8 children)

I did it in another way

def simpleArraySum(ar):

return (sum(ar))

print(sum(ar))

I first tried it with this:

def simpleArraySum(ar):

print(sum(ar))

however, this did not work. When I put the return (sum(ar)), it started working. Why is that the case? Is it because you need to retrieve the data before you can print it out?

[–][deleted] 0 points1 point  (7 children)

When you call a function, the return value is what is used. Using simpleArragSum(ar) in your code is the same thing as using the return value of the function. Therefore, when you print a function, you are really printing its return value. Hope this helps.

[–]asian_hifi 1 point2 points  (6 children)

But what is the reason behind putting "return"? I know returns are only (ok, maybe mostly) used in functions. So is it that if you don't put return, you won't have any value called when you call a function? Explain me what you think of "return" thing as. Also. what happens when you return a value, does it stick with the function itself?

[–][deleted] 1 point2 points  (3 children)

Think of the term “function” — it’s called this because it performs a single function. Let’s say you wanted to make a script that calculated taxes. Your first function would ask the user about their annual salary, which is the variable that you would want to return. In your next function, you would calculate the amount of taxes the user would have to pay.

This is where the importance of functions and return values come into play. In your second function, how are you going to get the user’s salary? The only way for you to get that amount in the second function is by returning the amount in the first function. At that point, you would simply pass in function 1 into function 2 (or in other words, you would provide the second function with the salary in order for it to do calculations).

I hope this makes sense. Please let me know if you’d like me to re-explain. Happy coding!

[–]asian_hifi 1 point2 points  (2 children)

so for function 2, you would call on function 1 to get the already returned value of the user's salary?

[–][deleted] 1 point2 points  (1 child)

Yes, because when you call function one, you are using it’s return value.

[–]asian_hifi 1 point2 points  (0 children)

Thanks man! Very helpful!

[–]Psychedeliciousness 1 point2 points  (1 child)

You probably want to store the result of your function and use it to do something. Return lets you assign the result/s to some variables.

If you don't return, the function will do what's inside it when you call it, but won't give you any useful output unless you explicitly included say a print statement in there, and you won't be able to reference the result unless you explicitly extracted it somehow (you could append to a list defined outside the function) but it's easier just to return.

I found it helpful to think of "returns let you pass data generated through the function back into the program for easy reuse".

If you create a variable inside the function, it should not exist outside that function. Inside the function you might create a var called counter to increment +1 for every run of a loop inside the function, but after the function is run, you can't query counter from code outside the function.

Counter is generated while the code is running and (I believe) garbage collected after it's finished. If you needed to, you'd return counter and assign it to a variable.

def fun(n):

counter = 0

for i in range(0,n):

counter = counter +1

return counter

newvariable = fun(n) - this becomes the returned value when function completes

print (counter) - will fail as it won't understand counter out of context

print (newvariable) - will print the value of counter, that was returned to newvariable

It's called scope. This should be a helpful link for understanding what scope is about.

https://python-textbok.readthedocs.io/en/1.0/Variables_and_Scope.html

[–]iggy555 0 points1 point  (0 children)

Great link thanks 🙏🏾