簡體   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