Keeping a string without over writing it throughout the program in python
Question: I'm trying to make a program that stores a user's recipe, using a tkinter gui
to do so. I need to make a way to keep track of what is inputted, and store it
in a text file. I have tried using lists to no avail, and think that using a
string is the way forward, but have run into a problem - each time I try to
add to the string, it over writes and doesn't keep the data from before. I
have tried to use

    
    
    mystring.join(a + b + etc)
    

but that didnt work, and my new code is as follows:

    
    
    from tkinter import *
    
    number_people = 1
    itemslist = ''
    itemslist1 = ''
    
    def script ():   # Puts main body of program into a function so that it can be re-run #
    
        global number_people
        number_people = 1
    
        global itemslist, itemslist1
        itemslist = ''
        itemslist1 = ''
    
        #### MAIN ####
    
        fake_window = Tk()                          #                                   #                             
        new_recipe_window = fake_window             # Opens window, allows it be closed #
        start_window = fake_window                  #                                   #
        start_window.title("Recipe Book Task")      #                                   #
    
    
        #### MAIN ####  
    
    
    
        ### Functions ###
    
        def close (x):
    
            global start_window
            global new_recipe_window
            (x).withdraw()
    
        def moreitems ():
    
            a = item_box.get()
            b = quantity_units_box.get()
            c = len(a)
            if a == '':
                pass
    
            elif b == '':
                pass
    
            else:
                item_box.delete(0,c)
                quantity_units_box.delete(0,c)
                global itemslist
                global itemslist1
                itemslist1 = itemslist + a + ', ' + b + ', '
                print ("Items list =", itemslist1)
    
    
        def new_recipe ():
    
            new_recipe_window = Tk()
            new_recipe_window.title("New Recipe")
            close(start_window)
    
            recipe_name_label = Label(new_recipe_window, text="Recipe Name: ")
            recipe_name_label.grid(row=0, column=0)
    
            recipe_name_box = Entry(new_recipe_window)
            recipe_name_box.grid(row=0, column=1)
    
            def continue_1 ():
    
                global check_box
                check_box = recipe_name_box.get()
                if check_box == '':
                    pass
    
                else:
                    global itemslist
                    global itemslist1
                    itemslist1 = itemslist + check_box + ', '
                    print (itemslist1)
                    continue_button_1.destroy()
    
                    item_label = Label(new_recipe_window, text="Ingredient: ")
                    item_label.grid(row=1, column=0)
    
                    global item_box
                    item_box = Entry(new_recipe_window)
                    item_box.grid(row=1, column=1)
    
                    quantity_units_label = Label(new_recipe_window, text="Quantity and Units: ")
                    quantity_units_label.grid(row=2, column=0)
    
                    global quantity_units_box
                    quantity_units_box = Entry(new_recipe_window)
                    quantity_units_box.grid(row=2, column=1)
    
                    def continue_2 ():
                        check_box_1 = item_box.get()
                        check_box_2 = quantity_units_box.get()
                        if check_box_1 == '':
                            pass
    
                        elif check_box_2 == '':
                            pass
    
                        else:
                            global itemslist
                            itemslist.join(check_box_1)
                            itemslist.join(check_box_2)
    
                            continue_button_2.destroy()
                            more_items.destroy()
    
                            add_people_label = Label(new_recipe_window, text="Choose amount of people")
                            add_people_label.grid(row=3, column=0, columnspan=2)
    
                            def add ():
                                global number_people
                                number_people += 1
                                num_people_label.config(text="Number of people: " + str(number_people))
    
                            def minus ():
                                global number_people
                                if number_people > 1:
                                    number_people -= 1
                                    num_people_label.config(text="Number of people: " + str(number_people))
    
                            def finish ():
                                itemslist.join(str(number_people))
                                print("ItemsList = " + itemslist)
                                saveFile = open("Recipe_Book.txt", "a")
                                saveFile.write(itemslist + '\n')
                                saveFile.close
                                close(new_recipe_window)
                                script()
    
                            num_people_label = Label(new_recipe_window, text="Number of people: " + str(number_people))
                            num_people_label.grid(row=4, column=0, columnspan=2)
    
                            add_people_button = Button(new_recipe_window, text="+")
                            add_people_button.grid(row=5, column=1)
                            add_people_button.config(command=add)
    
                            minus_people_button = Button(new_recipe_window, text="-")
                            minus_people_button.grid(row=5, column=0)
                            minus_people_button.config(command=minus)
    
                            finish_button = Button(new_recipe_window, text="Finish")
                            finish_button.grid(row=6, column=0, columnspan=2)
                            finish_button.config(command=finish)
    
    
                    continue_button_2 = Button(new_recipe_window, text="Continue...")
                    continue_button_2.grid(row=3, column=0)
                    continue_button_2.config(command=continue_2)
    
                    more_items = Button(new_recipe_window, text="Add another item", command=moreitems)
                    more_items.grid(row=3, column=1)
    
    
    
            continue_button_1 = Button(new_recipe_window, text="Continue...")
            continue_button_1.grid(row=1, column=0)
            continue_button_1.config(command=continue_1)
    
    
        new_recipe = Button(start_window, text="New Recipe", command=new_recipe)
        new_recipe.grid(row=0, column=0)
    
    
    script()
    

So to recap, my question is how do I keep the string itemslist and itemslist1
from being overwritten, or is there another way I can do this?

**EDIT FOR AAAANTOINE**

I was about to clarify for you what I wanted, but I just figured out what I
was doing wrong, thanks for your help, you taught me what .join does, thanks.


Answer: Your code never actually assigns to `itemslist` other than at the beginning of
`script()`. The only time it ever appears on the left side of the assignment
operator is when it's being initialized.

You can probably change all instances of `itemslist1` to `itemslist` and have
a working program.

## Edit

On further review, I suspect that you think str.join(v) appends string `v` to
the str. That's not how join works.

    
    
    >>> s = 'something'
    >>> s.join('a')
    'a'
    

`join` takes a list as an argument and joins its contents together, with the
`str` instance as a separator. Typically, the source string would actually be
an empty string or a comma.

    
    
    >>> s.join(['a', 'b', 'c'])
    'asomethingbsomethingc'
    >>> ','.join(['a', 'b', 'c'])  # comma separation
    'a,b,c'
    >>> '-'.join(s)    # spell it out!
    's-o-m-e-t-h-i-n-g'
    

### How do I do it, then?

You append to strings using this syntax:

    
    
    >>> s = s + 'a'
    >>> s
    'somethinga'
    

(Or the shorthand version:)

    
    
    >>> s += 'a'
    >>> s
    'somethinga'
    

