Search code examples
pythontypeerrorbubble-sort

Bubble sort algorithm | 'str' object does not support item assignment - Python


The brief: I challenged myself to write a program, as asked in a textbook, to test my understanding and demonstrate proficiency in the 'bubble sort'. The problem asked to write a program that "allows the user to enter 20 names into a String array. Sort the array in ascending (alphabetical) order and display its contents." My initial code worked, however I wasn't satisfied and one unsuccessful attempted code change later, I am stumped for a viable solution.

The issue: I initially wrote the friendInput function with for a loop, that iterates 20 times (for listInput in range(20):), to allow for 20 individual inputs, respective to the 20 values for the user to input. Whilst this worked, I consider it quite tedious from a user perspective, especially with a range of 20, to have to keep typing, hitting return, then repeating for 20 values.

Original code:

def friendInput():                                               
    friendList  = []                                             
    for listInput in range(20):                                  
        friendList.append(input("Please enter a friends name: "))      
    print("You added these to the list: "+' | '.join(friendList))
    return friendList   

Attempted code re-write:

def friendInput():
    friendList = []
    separator = ","
    friendList = input("Please enter friend's names, separated with commas: ")
    print("You added these to the list:",friendList.split(separator))
    return friendList

I did some research and learnt a lot, including the .split() function and tried to implement this, unsuccessfully it appears. Bubble sort algorithm:

def bubbleSort(friendList):
    for friends in range (0, len(friendList) - 1):
        for x in range (0, len(friendList) - 1 - friends):
            if friendList[x] > friendList[x+1]:
                friendList[x], friendList[x+1] = friendList[x+1],  friendList[x]
    return friendList

Error message: Everything seemed logical in my head, until the debugger threw back a TypeError: 'str' object does not support item assignment. The TypeError: goes on to quote part of my bubbleSort function - friendList[x], friendList[x+1] = friendList[x+1], friendList[x]

Where I need help: I suppose the error is really just saying that I'm trying to access and change a string, which is obviously disallowed, as it's not immutible. I am struggling to find a code change that would look clear, concise and not burden the user.

Can someone kindly offer suggestions, to remedy this error?


Solution

  • Re-read these lines:

    friendList = input("Please enter friend's names, separated with commas: ")
    print("You added these to the list:",friendList.split(separator))
    return friendList
    

    You are creating a string friendList = input... and then you are returning the string (return friendList). You intend to create a list and then return the list.

    Try this instead:

    friendList = input("Please enter friend's names, separated with commas: ")
    friendList = friendList.split(separator)
    print("You added these to the list:",friendList)
    return friendList