繁体   English   中英

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

[英]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)

排序时出错? 我该如何解决? 第24行,在主要students.sort()中TypeError:不可排序的类型: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)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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