任务读取......
编写一个程序,其中包含学生姓名列表并对其进行排序 创建一个类卷。名单将在一行上给出 由一个空格隔开。
所以我有我的代码。
items=input("Students: ")
items.sort(lambda x, y: cmp(x.lower(),y.lower()))
print(items)
为什么我得到这个,“AttributeError:'str'对象没有属性'sort'”错误“
Cheer's In Advanced
罗尼
答案 0 :(得分:7)
input()
返回一个字符串。如果您希望items
成为列表,则可以执行item.split()
:
我们假设items
为John Mary Bill
然后你可以这样做:
items = items.split()
然后执行items.sort()
,因为items
将是列表对象,而不是字符串。