简体   繁体   English

在 python 中的两个函数之间共享数据

[英]sharing a data between two functions in python

so i have a code which loops until the user types cancel and allows the user to input things into a list and then see the list if they want to.所以我有一个代码循环,直到用户键入取消并允许用户将内容输入到列表中,然后如果他们愿意查看列表。 but i have a problem wherein if i want to see the list the user has to input again and the data i typed when i added to the list is gone.但我有一个问题,如果我想查看列表,用户必须再次输入,而我添加到列表时输入的数据已经消失。 here is the code pls help me im pretty new to python.这是代码请帮助我对 python 很陌生。

answer=""
def addlist():
     list=input("what would you like to add? ")
     print("added successfully")
     return list
def showlist():
     list=addlist()
     print(list)
while answer !="cancel":
     answer=input("what would you like to do?, 1 for add to list, 2 for show list, cancel to close")
if answer=="1":
     addlist()
elif answer=="2":
     showlist()
else:
     print("wrong value")

So we've got a few problems here:所以我们在这里遇到了一些问题:

  1. addlist doesn't actually create a list, it creates a string. addlist 实际上并没有创建一个列表,它创建了一个字符串。
  2. showlist depends on addlist, when really we should be looking for a common list that we toss everything into (assuming you actually want to work with a list) showlist 依赖于 addlist,当我们真的应该寻找一个我们将所有东西都扔进的公共列表时(假设你真的想使用一个列表)
  3. Your indentation in your while loop is incorrect您在 while 循环中的缩进不正确
def addlist():
    item = input("what would you like to add?")
    return item


def showlist(mylist):
    print(mylist)

mylist = []
answer = ""
while answer != "cancel":
    answer = input("what would you like to do?, 1 for add to list, 2 for show list, cancel to close")
    if answer == "1":
        add_item = addlist()
        mylist.append(add_item)
        print("added successfully")
    elif answer == "2":
        showlist(mylist)
    else:
        print("wrong value")

That above seems to do the trick.以上似乎可以解决问题。

You seem to have a grasp on return statements, so how about you pass each of those functions a common list as I did mylist and have them do stuff with that.您似乎掌握了 return 语句,那么您如何将这些函数中的每一个传递给一个公共列表,就像我做mylist一样,并让它们做一些事情。

I changed addlist to actually just get the item we want to add to the list and then return that item and append it outside of the function.我将addlist更改为实际上只是获取我们想要添加到列表中的项目,然后将该项目和 append 返回到 function 之外。 In showlist I pass mylist to it via: showlist(mylist) and then print the list I get in that function.showlist中,我通过以下方式将mylist传递给它: showlist(mylist) ,然后打印我在 function 中获得的列表。

You should create a list element in order to append your items in it.您应该创建一个列表元素,以便 append 您的项目在其中。 You can see more here .你可以在这里看到更多。 One way to do what you want is like this:做你想做的一种方法是这样的:

answer = ""
temp_list = []

def addlist():
    item = input("what would you like to add? ")
    temp_list.append(item)

    print("added successfully")

def showlist():
    print(temp_list)


while answer != "cancel":
    answer = input(
        "what would you like to do?, 1 for add to list, 2 for show list, cancel to close")
    if answer == "1":
        addlist()
    elif answer == "2":
        showlist()
    else:
        print("wrong value")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM