Have a bubblesort program that I would like emails to be sorted into alphabetical order. However when I run the program with the List I would like to be sorted I get the error TypeError: 'tuple' object does not support item assignment. Here is the code:
def BubbleSort(logindata):
NoSwaps = 1
N = len(logindata)
while NoSwaps == 1:
Count = 1
NoSwaps = 0
for Count in range(N-1):
if logindata[Count] > logindata[Count+1]:
temp = logindata[Count]
logindata[Count] = logindata[Count+1]
logindata[Count+1]=temp
NoSwaps=1
return logindata
if __name__ == "__main__":
logindata=["tom@gmail.com","Password1"],["Harry","Password2"],["Jake","Password3"]
BubbleSort(logindata)
print(logindata)
The program sorts a list without the 'Passwords'. How would I get the code to run and successfully sort it.
With assuming that your Bubble sort algorithm works correctly , Your problem is you can not assign item in tuple
, when try logindata[count] = logindata[count+1]
, you can change logindata
to list
then return the tuple
of logindata
,and also you should capture returned logindata
and then print it,the whole code looks like this:
def BubbleSort(logindata):
NoSwaps = 1
N = len(logindata)
logindata = list(logindata)
while NoSwaps == 1:
Count = 1
NoSwaps = 0
for Count in range(N-1):
if logindata[Count] > logindata[Count+1]:
temp = logindata[Count]
logindata[Count] = logindata[Count+1]
logindata[Count+1]=temp
NoSwaps=1
return tuple(logindata)
if __name__ == "__main__":
logindata=["tom@gmail.com","Password1"],["Harry","Password2"],["Jake","Password3"]
logindata = BubbleSort(logindata)
print(logindata)
#(['Harry', 'Password2'], ['Jake', 'Password3'], ['tom@gmail.com', 'Password1'])