Functions assume variables are local and assigning a value will by default create a new local variable to contain it. The non-local, global variable remains unaffected.
This is desirable. Otherwise you'd have to create unique names for everything and always risk accidentally overwriting an important global variable.
You can change this behaviour by explicitly telling Python that it's the global variable that you want to assign a value to. For that, use the global
keyword to explicitly link the local name to the global variable:
def something():
global word
if number_a > number_b
word = 'hello'
else:
word = 'goodbye'
Sometimes this is necessary. In your simple example, however, it would probably be better to just return the value and do the assignment outside of the function. That way the function isn't tied to the surrounding namespace:
def something(number_a, number_b):
if number_a > number_b
return 'hello'
else:
return 'goodbye'
word = something(a, b)