[英]Can someone help me understand how this Python code works
This is code is an from a lesson i'm doing in class at school and i'm confused about how the list gets printed because it keeps ending with "None"... 这是我在学校上课时的一堂课的代码,我对列表的打印方式感到困惑,因为它一直以“ None”结尾。
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)
The results are: 结果是:
>>> 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
My real problem is... What's the None at the end?? 我的真正问题是...末尾没有什么? Where does it come from?
它从何而来? How can I fix it?
我该如何解决?
In the function printlist
you print the result of the function printlistproperly
, which returns nothing. 在功能
printlist
打印功能的结果printlistproperly
,它没有返回。 Therefore you get the None
. 因此,您得到
None
。 Solution: Just call printlistproperly(myList)
(without the print
). 解决方案:只需调用
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.