开发者

Print a list in another function in Python

I want to print myList in def b():. I used the following code:

def a():
    myList = []
    name = myList.append(raw_input("my name"))
    return myList

def b():
   开发者_如何学JAVA newList=a()
    print newList

But it isn't working. What do I need to change to be able to do this?


"something" is a string, not a list. However ["something"] would be a list of 1 string.

Your code is basically working, just don't forget to call b() somewhere if you want to display the result.

EDIT: Once again, it does work! Except that your're using raw input, so that you expect input from your user.

Run your b function, it will expect some input from your user : just type Florent[return]. your printed list will contain ['Florent']

Most probably, you need to read some good book about generic programming then python before asking questions...


This works

def a():
    myList = []
    name = myList.append(raw_input("my name"))
    b(myList)

def b(lst):
    newList=lst
    print newList

a()

Change "raw_input" to "input" for python3 and above.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜