为什么我在 str.sort 上收到 AttributeError?

Why do I get an AttributeError on str.sort?

这是我的代码:

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

让用户输入姓名列表并按字母顺序排序,但我一直收到此错误

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

有人能帮帮我吗

您尝试对 band 进行排序,它是一个字符串,因此在 Python 中是不可变的。这可能是一个错字,你想 bands.sort()