我有以下代码抛出和错误。

时间:2017-11-26 23:26:24

标签: python python-3.x

# 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)

排序时出错?我该如何解决?第24行,主要 students.sort()TypeError:unorderable类型:list()&lt; 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)

1 个答案:

答案 0 :(得分:0)

问题出在这一行:students.append(students)。这会将当前students列表添加到其自身的末尾,这在此上下文中没有任何意义。如果要解决此问题,请删除该行。