There is something wrong with my code. I still don't understand how local variable works. The following is an example of a simple code. And I have a NameError issue. Why? How can I fix this? I need to fix this error without Global variables! And there must be variable(a) ! What should I do for the answer of this code to come out 21?
def first(a):
a = 0
b = 3
return b
def second(a):
a = 0
j = 7
return j
def result():
return first(b) * second(j) # <-NameError: name 'b' is not defined in python
print(result())
You should define what does your b and j mean in string 10: For python you are giving noting to function, but this function is asking for some value (a), it should be like this:
def first(a):
a = 0
b = 3
return b
def second(a):
a = 0
j = 7
return j
def result():
b = 123 # Defining what are we giving to our functions
j = 5
return first(b) * second(j) # Now it will work
print(result())
So you can use b and j value, and access return values:
b = 3
return b
j = 7
return j
Think like this, when you're returning some value, it fully replaces function with that value.