Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (2015) Shopping List App Second Shopping List App

A kind of error in the code

The difference between first and second image. After I save and close the list there are no spaces between items. http://www.gifyu.com/images/Untitled-18f42f.jpg After I open it again, I load the list and close it again, these spaces appear. http://www.gifyu.com/images/Untitled-257dec.jpg This's the code.

my_list=[]
price_list=[]

def help_list():
    print("""
Info:
To review the options type: 'help'.
To view the list type: 'show'.
To save the list type: 'save'.
To end the list type: 'done'.
To load the saved list type: 'load'.
""")

def append_list(list_item,price):
    my_list.append(list_item)
    price_list.append(price)

def show_list():
    print("""
Until now you have {} things to buy:
""".format(len(my_list)))
    for item in my_list:
        print("-> "+item)

def end_list():
    print("""
 You have {} things.
 These are: """.format(len(my_list)))
    for a, b in zip(my_list,price_list):
        print("{0} -> {1}".format(a,b))
    total=sum(float(item) for item in price_list)
    print("""
Total = {} $.""".format(total))

def save_list():
    with open("to_shopp.txt","w") as file_saved:
        for a,b in zip(my_list,price_list):
            file_saved.write("{0} ->{1}\n".format(a,b))
    print("Your list is saved!")

def load_list():
    with open("to_shopp.txt") as f:
        for line in f:
            (a,b)=line.split(" ->")
            my_list.append(a)
            price_list.append(b)

def shoping_list():
    help_list()
    print("Your shopping list:: ")
    while True:
        list_item=input("-> ")    
        if list_item == "done":
            break
        elif list_item == "help":
            help_list()
            continue
        elif list_item == "show":
            show_list()
            continue
        elif list_item == "save":
            save_list()
            continue
        elif list_item == "load":
            load_list()
            continue
        while True:
            try:    
                price=float(input("Amount: "))
                break
            except ValueError:
                print("Use only numbers!")

        append_list(list_item,price)

    end_list()

shoping_list()

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

I haven't tested your code, but it seems during load_list, the lines read from the file will still have the end-of-line \n written to the file during save_list. It would seam if you save-load-save-load repeatedly, the number of extraneous newlines will keep increasing. You'll need to strip off the read EOL before appending to load_list.

Thank you very much. That was the problem. I solved with this:

def load_list():
    with open("to_shopp.txt") as f:
        for line in f:
            (a,b)=line[:-1].split(" ->")
            my_list.append(a)
            price_list.append(b)

For the first two attempts console not giving me any error... I've never tried a third time.