Im having trouble with the scopes of a function

hi so i pasted the code below im having an issue when it comes to saving the result my function takes in 2 arguments but the second one is a constant so it never changes. however due to the scope of the program, i decided to make another in the global scope have access to it in the bomb_enegry variable. is there any other way to rewrite this function in way that i dont have to repeat creating the same variable twice?

speed_of_light = 3*10**8
def get_energy(mass, c):
  c = 3*10**8
  return mass * c**2

bomb_energy = get_energy(bomb_mass, speed_of_light)
print(bomb_energy)

If you have a variable declared in the global scope, like you do currently, than you can use it in your function with out passing it as an argument.

number = 5

def log_number():
  print(number) #prints 5

Another way you could do it, would be to create a default argument for your function. In doing so you can then call the function without inputting an argument:

#              If n2 is ommitted when the function is called it will
#              input 5 as the value of n1
#              /
def add(n1, n2=5):
  return n1 + n2

print(add(3)) 
#         \
#         Even with n2s value ommited it will still function and return 8

Though I think this is kind of besides the point.

3 Likes

@8-bit-gaming is giving a good hint, but we should know that when the function takes a parameter and then suddenly decides to change its value, it kind of points to not needing the parameter in the first place.

The thing to do is remove the first line in your function (the one that assigns to c.

2 Likes

the steps in the question said to add both those parameters

Exactly, and you are passing two arguments, one of which is c (locally). There is no need to set it again.

1 Like

this was the question I was working on when creating this function. is it a miss understanding on my end? I basically got stuck on 9 due to the scope of the variable I couldn’t access it so I just made a global one.
8.

Define a function called get_energy that takes in mass and c .

c is a constant that is usually set to the speed of light, which is roughly 3 x 10^8. Set c to have a default value of 3*10**8 .

get_energy should return mass multiplied by c squared.

Test get_energy by using it on bomb_mass , with the default value of c . Save the result to a variable called bomb_energy .

The function takes two arguments… The rest mass, and the speed of light.

Once the values are inside the function we can return the computed value for energy.

speed_of_light = 3 * 10e8  # m/s

def energy(m, c):
    return m * c ** 2    # J

print (energy(bomb_mass, speed_of_light))
1 Like

That seems like strange wording, I would imagine the lesson still wants you to pass the value to the function instead of assigning it within. Could you post a link to the lesson? I am curious to see the wording.

https://www.codecademy.com/paths/computer-science/tracks/cspath-intro/modules/cspath-python-functions/projects/physics-class

I think I better understand now. I didn’t see that you had added the global variable speed_of_light yourself.

It seems they are wanting you to give c a default value, not assign it in the function:

c is a constant that is usually set to the speed of light, which is roughly 3 x 10^8. Set c to have a default value of 3*10**8.

Which is actually along the lines of what I stated earlier:

def add(n1, n2=5):
  return n1 + n2

print(add(3)) # prints 8
1 Like

ahh I see that makes sense when thinking of it that way, so I misunderstood the question lol thanks so much for helping me improve my code

1 Like

Also, we should respect that in that equation, c is a constant so needn’t be a parameter. The global value we give it is accessible inside the function. The only thing making it a parameter offers is to shorten the name of the variable.

return mass * speed_of_light ** 2

Parameters are local names. The global names can be anything, but our function knows them by the parameters.


Off topic, slightly

Declaring constants in Python is something I’ve not yet come across. Looking at how Ruby handles it, we might do the same in Python. It uses a method.

def speed_of_light():
    """
    space-time constant
    represented in meters per second
    """
    return 3 * 10e8

Mind, with Ruby we don’t need to invoke it, just call it by reference. In Python we will need to invoke it. Not a big deal though. Now we don’t have a global, but a globally available function. Easy-peasy.

def energy(mass):
    c = speed_of_light()
    return mass * c ** 2
2 Likes