1

Yes, I've read tons of examples on this but I still don't get it.

I'm learning Python and I made this script in order to help me understand how to 'return':

def random_number():
    random = 0
    random = (random + 5) * 10
    return random

def calc_pay_rise():
    payrise = 5
    random_number()
    payrise = payrise + random
    print payrise

calc_pay_rise()

I expect the output of 55. Instead I get the error: 'global name 'random' is not defined'. I think I defined what random is in the random_number() function though.

Is there a way to pass the value of random to the function calc_pay_rise()?

Ry-
  • 218,210
  • 55
  • 464
  • 476
BBedit
  • 7,037
  • 7
  • 37
  • 50
  • `random` is a variable local to `random_number()`. Seems like you’re looking for `payrise = 5 + random_number()`. – Ry- Sep 29 '13 at 17:19
  • Please don't call a variable `random`. [random](http://docs.python.org/2/library/random.html) ist the name of a module from the standard library and you (or an other coder) might get confused. – Matthias Sep 29 '13 at 17:24

5 Answers5

2

Try this:

payrise = payrise + random_number()

When you call a function that returns a value (a random number in this case), you're supposed to do something with the returned value.

Also notice that the variable random that was defined locally inside random_number() can not be "seen" from other functions - unless you declare a new variable locally, but it can have any name you want. Another way to say the same would be:

random = random_number()   # can be any name, I'm using `random` again
payrise = payrise + random
Óscar López
  • 232,561
  • 37
  • 312
  • 386
2

In calc_pay_rise, you are throwing away the value returned by the random_number() call; instead, do random = random_number(). What you've misunderstood is how variables work—local variables in one function (e.g. def random_number) are not visible in other functions (e.g. def calc_pay_rise).

def random_number():
    random = 0
    random = (random + 5) * 10
    return random

def calc_pay_rise():
    payrise = 5
    random = random_number() # assign the value to a local variable
    payrise = payrise + random
    print(payrise)

calc_pay_rise()

We can simplify this code by directly returning the value instead of assigning it to a variable first, and by using the result of the call directly in an expression instead of assigning it first:

def random_number():
    random = 0
    return (random + 5) * 10

def calc_pay_rise():
    payrise = 5
    payrise = payrise + random_number()
    print(payrise)

calc_pay_rise()

The key point here is that the function returns a value, not a variable. (Of course, the entire example can be simplified to print(55), but that's beside the point.)

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
  • You could further eliminate some dead code by just changing `random_number()` to `return 50`. – kindall Sep 29 '13 at 17:40
  • Well, clearly, but you could just inline `random_number` itself, and then `calc_pay_rise`, and then since its return value is not used, eliminate the call, and end up with an empty program. – Erik Kaplun Sep 29 '13 at 17:43
  • @ErikAllik I thought random = 0 is needed as I need to define the variable. It's very demoralizing, as I'm not understanding anything basically... not sure if this is normal. – BBedit Sep 29 '13 at 17:51
  • @user2071506: but you are initializing it to `0` and then using the variable to create a new value (that uses that `0`) and immediately assign it back to that variable—so what's the point? Why not just initialize the variable to the desired value immediately? Think about it. – Erik Kaplun Sep 30 '13 at 09:24
  • "I thought random = 0 is needed as I need to define the variable." See also: https://stackoverflow.com/questions/11007627/ – Karl Knechtel Jul 04 '22 at 21:57
2

you need to set a variable named "random"

so your calc_pay_rise function should look like:

def calc_pay_rise():
    payrise = 5
    random = random_number()
    payrise = payrise + random
    print payrise
Nope
  • 34,682
  • 42
  • 94
  • 119
1

your variable "random" in def calc_pay_rise() was never assigned. Try:

def calc_pay_rise():
    payrise = 5
    random = random_number()
    payrise = payrise + random
    print payrise
Christian David
  • 186
  • 1
  • 3
1

I'm not a python programmers, but this looks like a problem of scope. You did define the variable "random" but it's scope-- the section of the code where it is visible and usable-- is limited to the function where you define it. In this case, inside the random_number() function.

It is not visible inside the calc_pay_rise() function.
What is visible is the other function, random_number() itself. That is what you need to use in your addition.

That is the basic point of returning values.

Novak
  • 4,687
  • 2
  • 26
  • 64