Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How would I be able to delete an element in my shopping list and delete the \n with it so it moves the list up and doesn't leave gaps

I am trying to make a shopping list, however every time I delete an item it leaves a big gap and looks very awkward, please can someone help, I am still very new to it all and I have only been learning a month or two so a simple answer would be very much appreciated:

def subtract(user):
    x = 0
    while x == False:
        answer = input("What would you like to subtract?")
        if answer in user:
            user = user.replace(answer,"")
            y = 0
            print("your list is:" + user)
            add_or_subtract(user)
            x == True
            break
        else:
            print ("sorry, I do not recognise this, please try again.")

def add(user):
    x = 0
    while x == False:
        answer = input(str("what would you like to add?".lower()))
        if answer in user:
            print ("sorry, this is already in the list")
        else:
            user = user + "\n" + answer
            print ("Your list is: " + user)
        answer = input("are you finished adding?".lower())
        if answer in ("yes", "yea", "ye"):
            answer1 = input("do you want to subtract or finish?")
            if answer1 in ("subtract"):
                subtract(user)
                x == True
                break
            elif answer1 in ("finish"):
                print (user)
                x == True
                break
            else:
                print("Sorry I do not recognise this")


def add_or_subtract(user):
    x = 0
    while x == False:
        answer = input ("do you want to add or subtract or finish?".lower())
        if answer in ("add","addition"):
            add(user)
            x == True
            break
        elif answer in ("subtract","takeaway"):
            subtract (user)
            x == True
            break
        elif answer in ("complete", "finish"):
            print ("your final list is: " + user)
            x == True
            break
        else:
            print ("sorry i do not recognise this")
        
        
def initial(user):
    x = 0
    while x == False:
        user = input("please type out your first item:".lower())
        content = input(str("your first item is "+ user + " are you content with that?".lower()))
        if content in "no":
            user = ""
            continue
        elif content in "yes":
            add_or_subtract(user)
            x == True
            break
                        
                        
shopping = ""
initial(shopping)

Also is there a better way of doing this, I feel like there is, could I use a list to record all the items and is there a way I can store the existing list into a database and then re-use that when updating?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The reason why you have a gap is because you are forgetting the \n character (newline character), so when replacing put '\n' before answer:

def subtract(user):
    x = 0
    while x == False:
        answer = input("What would you like to subtract?")
        if answer in user:
            user = user.replace('\n'+answer,"") # where it is edited
            y = 0
            print("your list is:" + user)
            add_or_subtract(user)
            x == True
            break
        else:
            print ("sorry, I do not recognise this, please try again.")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading