Hi I am working on a module for Grok learning and it gave me a problem to create a program that takes a list of students names and prints each name out individually and alphabetically as a class roll (each name capitalized).
#pseudo
# ask for students and store in 'students'
# split 'students', which is now a list
# sorts the list alphabetically
# print('Class roll')
# for i in students
# print i . capitalize
data = input('Students: ')
students = data.split()
students = students.sort()
print('Class Roll')
for i in students:
print(i.capitalize())
and essentially it gives me this error message that I dont understand:
Students: a c b d
Class Roll
Traceback (most recent call last):
File "program.py", line 13, in <module>
for i in students:
TypeError: 'NoneType' object is not iterable
Comments are fine.
CodePudding user response:
When you call sort
on a list it sorts in place, rather than returning a sorted list. Try it without assignment, i.e.:
data = input('Students: ')
students = data.split()
students.sort() # this line is changed
print('Class Roll')
for i in students:
print(i.capitalize())
CodePudding user response:
This should fix your problem. Simply remove the equal sign since .sort() transforms the list itself.
data = input('Students: ')
students = data.split()
students.sort()
print('Class Roll')
for i in students:
print(i.capitalize())