![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
OK, i am making a simple program in python, all was going well until the variables didnt work. The actual code is a bit to long to post and it is very messy, so i made this example of what error i am getting.
#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"
changeit("Goodbye")
#Printing the variable to check that it has changed acording to the function that we called
print msgYou see basically, i have declared a variable, i then have made a funtion that should change that variable, i then call that funtion and print the variable. Basically, the msg variable stays as "Hello" and doesnt change. I think it is something to do with Global variables etc. but im not sure how to correct it, i tried return msg but it doesn't work. I have searched google (maybe i am searching for the wrong words?) and i checked the python documentation but i cannot find the answer to my question. How can i fix it so that it prints the variable from the function? ![]() |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
You have a function that returns something so you need to assign it to a variable when you call it. This should do it:
#defining the variable
msg = "Hello"
#function to change 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")
#Printing the variable to check that it has changed acording to the function that we called
print msg
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
#3 |
|
Programming Guru
![]() ![]() |
#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 msgReturn 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
__________________
Looking for tough programming challenges? Try participating in Sane's Monthly Algorithms Challenges! Composing Techno is a little side hobby of mine. Techno by DJ Sane. All free for download. |
|
|
|
|
|
#4 |
|
Expert Programmer
|
Thanks for your help, just after i posted this topic, "the python bible" book form amazon.co.uk got delivered and i learnt how to do object oriented python, so i managed to sort out my problem.
![]() It was a stupid error to make in the first place, thanks dietrich for sorting that out. Sane that advanced way looks good, mine looks a bit like that ![]() |
|
|
|
|
|
#5 |
|
Programming Guru
![]() ![]() |
Hey I sorted it out too! Just 30 seconds after dietrich did .
![]()
__________________
Looking for tough programming challenges? Try participating in Sane's Monthly Algorithms Challenges! Composing Techno is a little side hobby of mine. Techno by DJ Sane. All free for download. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|