简体   繁体   中英

I have the following code throwing and error.

# This program records the names of 12 students in a list
# named students.
NUM_STUDENTS = 12

def main():
    # Create a list to hold the names of students.
    students = [0] * NUM_STUDENTS

    # Create a variable to hold an index
    index = 0

    print('Enter the names of 12 students.')

    # Add student names to the list.
    while index < NUM_STUDENTS:
        # Get the name of a student from the user.
        students[index] = input('Enter the name of a student: ')
        index += 1

        # Append a name to the student list.
        students.append(students)

Error when sorting? How do I fix this? line 24, in main students.sort() TypeError: unorderable types: list() < str()

    # Sort the list of name alphabetically.
    students.sort()
    print('Alphabetically sorted list of names:', students)

    # Reverse the list of names
    students.reverse()
    print('Reverse sorted list of name:', students)

The problem lies in this line: students.append(students) . That adds the current students list to the end of itself, which makes no sense in this context. If you want to fix this, remove that line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM