add to a string variable without deleting last assignment

Question:

strings = None
def test(a)

       strings = str(strings) +  f"{a} n"

when this function is called multiple times

test("hello")
test("world")

how do I allow strings to be equal to

"hello" n
"world"

At the moment it just picks up the last time a was passed so strings is "world"

Asked By: lunbox

||

Answers:

None cannot be concatenated with a String, Also strings is a global object and needs to be so declared or a local object will assigned within the function. Try:

    strings = ""
    
    def test(a):
        global strings
        strings  +=  f"{a} n"
    
    test("hello")
    test("world")
    
    print(strings)
Answered By: user19077881
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.