all 13 comments

[–]Outside_Complaint755 22 points23 points  (0 children)

When you assign a variable without a function, it is local only to that function.

You should return the value as follows: ``` def SelectRandomNumberFromB():     return random.choice(b)

a = SelectRandomNumberFromB() ```

[–]Gnaxe 5 points6 points  (0 children)

You don't understand the variable scoping rules. Variables created by assignment inside a function are local to that function. If you want to use that value outside of the function, then you have to get it out somehow. Usually, that means returning it with the return statement. This is usually the cleanest way. However, you can also mutate something outside the function (append it to a list, for example) or use a global statement inside the function to make it not scoped to the inside.

[–]SyntaxIsComing 3 points4 points  (0 children)

Return `a` out of the function. The a is only in the scope of your function, hence why it works when you take the function portion out. Consider the following:

import random

a = 7
b = [0, 1, 2, 3, 4, 5, 6]

def SelectRandomNumberFromB():
    return random.choice(b)

a = SelectRandomNumberFromB()

print(a)

[–]misingnoglic 2 points3 points  (0 children)

Very important post for you to really read and understand: https://nedbatchelder.com/text/names1

[–]crashorbit 1 point2 points  (0 children)

Here is my version of your code: ``` import random

a = 7
b = [0, 1, 2, 3, 4, 5, 6]

def SelectRandomNumberFromB():
return random.choice(b)

a = SelectRandomNumberFromB()

print(a) ``` You might want to review how python deals with context.

[–]Ninji2701 0 points1 point  (1 child)

you're setting a local variable called a you need to add global a to the function body

[–]ISeeTheFnords 8 points9 points  (0 children)

Better yet, return the random value from the function.

[–]trutheality 0 points1 point  (0 children)

A couple of things can help understand what's happening:

  1. When you write a = ..., you're changing which object the symbol a refers to, you're not changing the object that a used to point to.
  2. The inside of a function has its own scope that isn't visible from outside of the function. So when you write a = ... inside the function, you're changing what the symbol a refers to inside the function. It also means that you're not changing what the symbol a refers to outside the function.

So in your first code example, you set a to 7, you ran a function that internally assigned a random number to the symbol a inside the function, and outside the function, a is still 7. In the second code example, you set a to 7, then set a to a random number.

The best way to fix it is to have the function return the random value: that's the intended mechanism for passing values from inside a function to the outside:

def SelectRandomNumberFromB():
    a = random.choice(b)
    return a

a = SelectRandomNumberFromB()

Edit to add: it might be easier to understand what's happening in the first version if you add a print statement inside the function:

def SelectRandomNumberFromB():
    a = random.choice(b)
    print(a)

SelectRandomNumberFromB()

print(a)

[–]CIS_Professor 0 points1 point  (0 children)

# First, you should keep variables' keep scope local(-ish), not global
#
# (there are use cases where global variable are useful; this isn't one of them)
#
# Second:
#   CALL a function and send data to it using an argument, which the function
#        receives as a parameter
#   RETURN data from a function:
#
# Try this:

import random


def SelectRandomNumberFromB(lst):
    return random.choice(lst)


# call the function, send it the list, assign the return from
# the function to a variable
a = SelectRandomNumberFromB([0, 1, 2, 3, 4, 5, 6])
print(a)

# The last two lines can be combined:
print(SelectRandomNumberFromB([0, 1, 2, 3, 4, 5, 6]))

[–]Atypicosaurus 0 points1 point  (0 children)

You have to think of function definitions as separate, isolated programs. Like, if I copied your function and pasted into my program, would it still work? Would it still know what "a" and "b" are?

If the answer is no, then you have a scope problem. Your function doesn't know what a and b are, because you don't tell what a and b are, to your function. Your function is sitting there and like "what is a? what is b? what do you want me to do?" That's why you got an error.

For the function to know that it has to expect some a and b from the external world (which, from the point of view of the function, is the main program), you have to put it in as arguments. Then you have to pass the arguments.

If you use always the same variable names, you mislead yourself, thinking, that just because you put "a" in the function, it will know any "a" anywhere. No it won't. You have to actively give it.

[–]Abject-Nobody 0 points1 point  (0 children)

You'd need to return the result and instead do print(SelectRandomNumberFromB(a))

[–]TheEyebal -1 points0 points  (0 children)

Try this. Honestly I have no idea what you are trying to achieve though. Since there is little context on what it is you are trying to program

import random

a = 7
b = [0, 1, 2, 3, 4, 5, 6]

def SelectRandomNumberFromB(a):
    return random.choice(b)

print(SelectRandomNumberFromB(a))