Python Forum
List problem "TypeError: must be str, not int"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List problem "TypeError: must be str, not int"
#1
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:
#!/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?
Reply


Messages In This Thread
List problem "TypeError: must be str, not int" - by RedSkeleton007 - Jan-28-2018, 09:00 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 5,190 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  TypeError: unsupported opperand type(s) for %: 'int' and 'list' cool_person 7 2,458 May-07-2022, 08:40 AM
Last Post: ibreeden
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,882 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  Problem with "Number List" problem on HackerRank Pnerd 5 2,297 Apr-12-2022, 12:25 AM
Last Post: Pnerd
  You have any idea, how fix TypeError: unhashable type: 'list' lsepolis123 2 3,205 Jun-02-2021, 07:55 AM
Last Post: supuflounder
  TypeError: 'list' object is not callable Python_Mey 1 2,606 Aug-25-2020, 03:56 PM
Last Post: Yoriz
  Python Error- TypeError: ('Params must be in a list, tuple, or Row', 'HY000') DarkCoder2020 3 5,852 Jul-29-2020, 12:02 AM
Last Post: Larz60+
  [Solved]TypeError: list indices must be integers or slices, not str NectDz 3 4,094 Jun-02-2020, 08:21 AM
Last Post: DreamingInsanity
  TypeError: list indices must be integers or slices, not float hissonrr 2 3,453 Apr-19-2020, 12:02 AM
Last Post: hissonrr
  TypeError indexing a range of elements directly on the list JFerreira 2 2,329 Mar-30-2020, 04:22 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020