1

这是我在学校课堂上上的一堂课的代码,我对列表的打印方式感到困惑,因为它一直以“无”结尾......

def printlist(myList, pointer):
    print("The List is: ", myList)
    print("Pointer length: ", pointer)
    print("The List length is: ", len(myList))
    print("The List printed properly:")
    print(printlistproperly(myList))

def printlistproperly(myList):
    thelength = len(myList)
    for i in range(thelength):
        print(i, " ", myList[i])

def popin(myList,pointer):
    myList.append(input("Enter a value: "))
    pointer = len(myList)-1
    return myList, pointer

def main():
    myList = ["Ford","Toyota","Mustang"]
    pointer = len(myList)-1
    myList,pointer = popin(myList,pointer)
    printlist(myList, pointer)

结果是:

>>> main()
Enter a value: Dodge
The List is:  ['Ford', 'Toyota', 'Mustang', 'Dodge']
Pointer length:  3
The List length is:  4
The List printed properly:
0   Ford
1   Toyota
2   Mustang
3   Dodge
None

我真正的问题是......最后的None是什么?它从何而来?我该如何解决?

4

1 回答 1

10

在函数printlist中打印函数的结果printlistproperly,它什么也不返回。因此你得到None. 解决方案:只需调用printlistproperly(myList)(不带print)。

def printlist(myList, pointer):
    print("The List is: ", myList)
    print("Pointer length: ", pointer)
    print("The List length is: ", len(myList))
    print("The List printed properly:")
    printlistproperly(myList)
于 2013-01-22T12:26:28.330 回答