How do I include a first function (with a specific parameter) to a econd function (with a different specific parameter)?


On first glance, I know the following code looks incorrect.

Example A

def one_good_turn(n):
  return n + 1
def deserves_another(m):
  return one_good_turn(n) + 2

Here is the link for reference:
example function within a function’

In the actual example before hand, the python coure suggested doing the following:

Functions Calling Functions
We’ve seen functions that can print text or do simple arithmetic, but functions can be much more powerful than that. For example, a function can call another function:

def fun_one(n):
  return n * 5

def fun_two(m):
  return fun_one(m) + 7

My specific problem is that I want to use a different letter for the parameter than the first function.

So looking at example A,

I want def deserve another to have a different parameter name than def one good turn

How would I do that?


You already have different parameter names… m and n


ye, but the system says it is incorrect. My example (example A) is the one I am talking about. I must be doing something incorrect?


What does what system say when what is incorrect?

Your first piece of code is perfectly valid python. However, it might not run to completion. There’s a difference

For example, this is valid python:

1 / 0

However, it will crash

And, if you do this:

a = 3

Then you’ll get told b isn’t defined. That should not be surprising.

Or, perhaps closer to your example:

def f(a):

The whole point of names is that you can refer to them again later. If you use a different name then there’s no reason why it would be defined


I think the problem I am having i scope. Yet, I dont know how to fix that or how to get around it. Ive already worked my way through the javascript course (it took me four months!!) So I’m just trying to make sure I am understanding core programming principles like scope.

How do I allow the second function to add to the second one in example A?


That’s vague. Be more concrete and I just might have an answer. I suspect you can’t actually phrase this in a way that makes sense though, that you haven’t thought through what you mean. That’s the feeling I get anyway, I obviously don’t know the reality of that

If I was to just read what it says, you’re asking this:

def g(x):
    return x + 1

def f(x):
    return g + x  # error when attempted, can't add to a function

def one_good_turn(n):
  return n + 1
def deserves_another(m):
  return one_good_turn(n) + 2 ///this would be different if I just aid n+2 without defining n

The above demonstrates that I am adding 2 to whatever the outcome of one_good_turn is.
In my opinion, the parameter that is a different letter should not affect the ewuation…

however the system says that there is an effect?

Is there an error somewhere else?

I believe I am applying scope correctly. Your answer from before is not relevant - I dont think it is, anyway.

Hoping to discuss this.

def deserves_another(m):
  return one_good_turn(n) + 2


This code attempts to use obtain a value from a variable n
There is no n defined anywhere

If you do this:


You will get told n has not been defined, and that therefore the action you requested cannot be completed – NameError


I think you’re suggesting that when you say


Python should know that you mean


Without you having said so.

This would make programming impossible, because the computer would not be doing what it is told.


Would I add 4 to a parameter using the following syntax:

def one_good_turn(n):
  return n + 4

This is a follow up question that will allow me to independently solve to get to the point where I can add one function to another number, by the way


What do you think this should do?

def one_good_turn(n):
  return n + 1
def deserves_another():  # removed unused parameter m
  return one_good_turn(n) + 2


I removed deserves_another's parameter because it was ignored and had no purpose.

Or to simplify further, remove stuff that has nothing to do with it:

def one_good_turn(n):
  return n + 1

Further yet:

n + 1  # n is undefined, *can't* use it, doesn't exist


I believe 2 is being added to the prior function.

Is that correct?


A function doesn’t support addition, you can’t add 2 to a function:

def f():

return 2 + f

You can try, that is allowed. But it won’t succeed.

You’re a whole lot closer if you call the function first:

def f():

return 2 + f()  # now calling f, adding 2 to the result

The problem here is you use n before having defined it

def one_good_turn(n):
  return n + 1
def deserves_another():
  return one_good_turn(n) + 2  # NameError, n is undefined


That code doesn’t ever call one_good_turn, because it tries to get the value of n which doesn’t exist


That a function has a parameter does not cause that name to be defined anywhere else:

def f(x):
    return x + 1

print(x)  # NameError x has not been defined


How do I create a number (unknown except when I enter it) and then add to it?

That confused me when I did javascript on certain exercises and it is confusing me again.

Is this an example of what I can do?

def one_good_turn:
    my_sum = 0
    return my_sum + 1


def one_good_turn:
    my_sum = 0
   return my_sum+=1  /// may have done this incorrectly with +=

So it seems you need to declare a variable within each function OR globally at the top? CAN you declare a global variable in python at the top?


Python has no global namespace, you can create variables in the module though

What do you mean by create a number? Sounds like you’re asking about a random number generator, but even then you have to specify a range. So where would the number come from? No, values don’t spontaneously come into existence


I think you’re misunderstanding me about the random number generator - I did not intend to talk about a random number generator.

I’m not there yet, lol.

def one_good_turn(number):
    my_sum = 0 + number
    return my_sum

def one_good_turn:
    my_sum = 0
   return my_sum+=1  /// may have done this incorrectly with +=

Do the above work in python? Im epecially curious about the += operator


+= is a statement, not allowed there
Invalid syntax on the function statements too, missing parameter list
…and indentation

besides, we have different ideas of what’s right/wrong/allowed. Instead you’ll have to talk in terms of what should or should not happen


Could my new edit with one good turn (directly above) be correct. Like you aid, there’s probably different ways to make this work. But I’m trying to see if I understand the concept.


Why create a new variable to refer to the same value again?

Do this instead:

def one_good_turn(number):
    return number

(and why add 0)

def one_good_turn(number):
    return x

This is still correct, it’s valid python. But This function has a parameter that it doesn’t use, so it should instead be this:

def one_good_turn():
    return x