[SOLVED] Running the rest of the script with the function's string stuffs

Issue

I don’t know how I can describe it on the title, but basically here is what I would like to describe in detail. This is an example of what I’m trying to do:

def test():
     text = 'Hello World'
     print(str(text))

test(),
print ("Function text is " + text)

which I would like the last line to print out Function text is Hello World, but it won’t print out any way but showing NameError: name 'text' is not defined. Any ideas?

Solution

You should normally return from a function. This is captured in b.

def test():
     text = 'Hello World'
     return text

b = test()
print ("Function text is " + b)

Shorter one:

def test():
     return 'Hello World'
     
print ("Function text is " + test())
    

Answered By – Devang Sanghani

Answer Checked By – David Marino (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *