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”…
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
printlistyou print the result of the functionprintlistproperly, which returns nothing. Therefore you get theNone. Solution: Just callprintlistproperly(myList)(without theprint).