3

The Task Reads...

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.

So I have my code.

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

Why am i getting this, "AttributeError: 'str' object has no attribute 'sort'" Error"

Cheer's In Advanced

Ronny

1 Answer 1

9

input() returns a string. If you would like for items to be a list, you can do item.split():

Let's assume items is John Mary Bill

You can then do:

items = items.split()

Then do items.sort(), as items will be a list object, not a string.

Sign up to request clarification or add additional context in comments.

1 Comment

Cheers, you saved the DAY!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.