Jan-28-2018, 09:00 AM
I'm trying to print the elements of a list of strings to test Python's sorting methods, but it's complaining about data types:
And why is "Dink" the only thing printing out before generating an error?
#!/usr/bin/env python3 #ListSorting.py def stringListSort(): upperAndLowerSort = ["chicken", "pig", "Dink", "cow"] upperAndLowerSort.sort() for i in upperAndLowerSort: print(str(i))#print(upperAndLowerSort[i]) i += 1 keyArgSort = ["chicken", "pig", "Dink", "cow"] keyArgSort.sort(key=str.lower) for i in keyArgSort: print(i) i += 1 def main(): stringListSort() main()
Error:========= RESTART: I:/Python/Python36-32/SamsPrograms/ListSorting.py =========
Dink
Traceback (most recent call last):
File "I:/Python/Python36-32/SamsPrograms/ListSorting.py", line 19, in <module>
main()
File "I:/Python/Python36-32/SamsPrograms/ListSorting.py", line 17, in main
stringListSort()
File "I:/Python/Python36-32/SamsPrograms/ListSorting.py", line 9, in stringListSort
i += 1
TypeError: must be str, not int
>>>
I understand that I'm not printing integer elements, but how do I get it to print the string elements via their list index?And why is "Dink" the only thing printing out before generating an error?