#defining the variable
msg = "Hello"
#function to chnage the msg variable to the argument in the funtion
def changeit(new):
msg = new
return msg
#calling the function with the argument "goodbye"
msg = changeit("Goodbye") #This line was changed
#Printing the variable to check that it has changed acording to the function that we called
print msg
Return from a function returns the variable to whatever called the function. The way you had it setup, the new value wasn't being returned to any variable, and the temporary variables were therefore not used.
For this part:
def changeit(new):
msg = new
return msg
Could be made:
def changeit(new):
return new
Because declaring msg doesn't do anything anyways. Inside a function, only the variables that were changed and created within that function will operate, none will exist outside that function or the next time you visit the function again. The only way to ever see it again is with the command global, or using return and then passing it back as a paramater.
Here is another way of looking at your program that is slightly more advanced:
class variable(object): #variable can be changed to any word
#this will initiate upon declaring variable
def __init__(self, dec_variable):
#self is the collection of variables within the class, dec_variable is the paramater passed when the class is declared
self.text = dec_variable #self makes the variable able to be manipulated within the class, self.text = dec_variable is making "Hello" accessable by the other functions in this class
def changeit(self, new_variable):
#self is the collection of variables within the class, new_variable is the paramater passed when changeit is called
self.text = new_variable #self.text which was declared in a different function, can still be manipulated here. It is redeclared as "Goodbye"
#Declare msg as a reference of the class variable
msg = variable("Hello")
#Access the function changeit in the class variable (since variable is referenced with msg)
msg.changeit("Goodbye")
#Print the variable variable.text (since variable is referenced with msg)
print msg.text