Search code examples
pythonpython-3.xtraceback

Why do I get an AttributeError on str.sort?


This is my code:

bands = []
band = []
headline = input('Headline: ')
while band != "":
  bands.append(band)
  band = input('Band: ')
band.sort()
print(headline)
for band in bands:
  print(band)

its for the user to input a list of names and for it to sort it alphabetically but i keep getting this error

Traceback (most recent call last):
  File "program.py", line 7, in <module>
    band.sort()
AttributeError: 'str' object has no attribute 'sort'

could someone please help me.


Solution

  • You try to sort band which is a string and thus immutable in Python. This is probably a typo and you wanted to to bands.sort()