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

Question:

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?

Asked By: DannyMoham1

||

Answers:

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.")
Answered By: Pythoneer
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.