On Aug 11, 12:07 pm, fuglyducky <fuglydu...@gmail.com> wrote: > I am a complete newbie to Python (and programming in general) and I > have no idea what I'm missing. Below is a script that I am trying to > work with and I cannot get it to work. When I call the final print > function, nothing prints. However, if I print within the individual > functions, I get the appropriate printout. > > Am I missing something??? Thanks in advance!!!! > > ################################################ > # Global variable > sample_string = "" > > def gen_header(sample_string): > HEADER = """ > mymultilinestringhere > """ > > sample_string += HEADER > return sample_string > > def gen_nia(sample_string): > NIA = """ > anothermultilinestringhere > """ > > sample_string += NIA > return sample_string > > gen_header(sample_string) > gen_nia(sample_string) > > print(sample_string)
There are 2 problems with your program. (1) If you want to use a global variable in a function, you have to add the line "global sample_string" to the beginning of that function. (2) Once you do (1), you will get an error because you've got sample_string as a global and a function parameter. Which one do you want to use in the function? You should change the name of the parameter to "sample" to solve that confusion. Here's the result, which works for me: sample_string = "" def gen_header(sample): global sample_string HEADER = """ mymultilinestringhere """ sample_string = sample + HEADER return sample_string def gen_nia(sample): global sample_string NIA = """ anothermultilinestringhere """ sample_string = sample + NIA return sample_string gen_header(sample_string) gen_nia(sample_string) print(sample_string) -- http://mail.python.org/mailman/listinfo/python-list