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

huangapple go评论70阅读模式
英文:

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

问题

我正在尝试制作一个购物清单,但是每次我删除一个项目时,它都会留下一个大的间隙,看起来很尴尬,是否可以有人帮忙?我还是很新手,只学了一个月或两个,所以简单的答案将非常感激:

def subtract(user):
    x = 0
    while x == False:
        answer = input("您想要减去什么?")
        if answer in user:
            user = user.replace(answer, "")
            y = 0
            print("您的清单是:" + user)
            add_or_subtract(user)
            x = True
            break
        else:
            print("抱歉,我不认识这个,请再试一次。")

def add(user):
    x = 0
    while x == False:
        answer = input(str("您想要添加什么?").lower())
        if answer in user:
            print("抱歉,这已经在清单中了")
        else:
            user = user + "\n" + answer
            print("您的清单是:" + user)
        answer = input("您添加完毕了吗?").lower()
        if answer in ("yes", "yea", "ye"):
            answer1 = input("您想要减去还是结束?")
            if answer1 in ("subtract"):
                subtract(user)
                x = True
                break
            elif answer1 in ("finish"):
                print(user)
                x = True
                break
            else:
                print("抱歉,我不认识这个")

def add_or_subtract(user):
    x = 0
    while x == False:
        answer = input("您想要添加还是减去还是结束?".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("您的最终清单是:" + user)
            x = True
            break
        else:
            print("抱歉,我不认识这个")

def initial(user):
    x = 0
    while x == False:
        user = input("请键入您的第一项:".lower())
        content = input(str("您的第一项是 " + user + " 您对此满意吗?").lower())
        if content in "no":
            user = ""
            continue
        elif content in "yes":
            add_or_subtract(user)
            x = True
            break

shopping = ""
initial(shopping)

另外,是否有更好的方法来完成这个任务?我觉得有可能,我能否使用列表来记录所有的项目,并且是否有办法将现有的列表存储到数据库中,然后在更新时重新使用它?

英文:

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?

答案1

得分: 1

因为你忘记了\n字符(换行字符),所以出现间隙的原因是,在替换之前,在答案之前加上'\n'

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,"") # 这是被编辑的地方
            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.")
英文:

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

huangapple
  • 本文由 发表于 2023年2月10日 05:12:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404460.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定