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"
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)
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"
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)