AttributeError:“str”对象没有“sort”属性

2024-06-01 13:45:21 发布

您现在位置:Python中文网/ 问答频道 /正文

任务是。。。

Write a program that takes a list of student names and sorts them to create a class roll. The list of names will be given on a one line separated by a single space.

所以我有我的密码。

items=input("Students: ")
items.sort(lambda x, y: cmp(x.lower(),y.lower()))
print(items)

为什么我得到这个,“attribute Error:'str'对象没有属性'sort'”错误

欢呼声已经响起

罗尼


Tags: andoftothatnamesitemsprogramsort
1条回答
网友
1楼 · 发布于 2024-06-01 13:45:21

input()返回字符串。如果希望items成为列表,可以执行item.split()

假设itemsJohn Mary Bill

然后您可以:

items = items.split()

然后执行items.sort(),因为items将是一个列表对象,而不是字符串。

相关问题 更多 >