简体   繁体   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)

Error when sorting? 排序时出错? How do I fix this? 我该如何解决? line 24, in main students.sort() TypeError: unorderable types: list() < str() 第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)

The problem lies in this line: students.append(students) . 问题在于这一行: students.append(students) That adds the current students list to the end of itself, which makes no sense in this context. 这会将当前的students列表添加到其末尾,在这种情况下这没有任何意义。 If you want to fix this, remove that line. 如果要解决此问题,请删除该行。

暂无
暂无

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

相关问题 为什么下面的代码会抛出错误? - Why is the following code throwing an error? 我有一段代码来运行回归,但我不知道为什么会出现语法错误。 下面的代码 - I have a piece of code to run regression but I have no idea why I get a syntax error. the code below 我的代码有问题。 我需要解决这个错误。 帮我 - I have a problem with a code. I need to solve this error. Help me 这是我识别人脸的代码。 我收到以下错误。 我正在使用 dlib 形状预测器 - This is my code for recognition of faces. I am getting the following error. i am using dlib shape predictor 这是可视化SVM决策边界的代码。 我无法发现错误。 请看一看 - Here is the code to visualize SVM decision boundary. I am unable to spot the error. Please have a look 我有这段代码,试图通过捕获视频来运行社会隔离识别,但我有这个错误。 你可以帮帮我吗? - I have this code, trying to run a social isolation identification by capturing video, but I have this error. Could you help me? 为什么下面的代码会抛出 0 异常? - Why is the following code throwing an exception of 0? 在 Python 中加载 JSON 文件会引发错误。 - Loading JSON file in Python throwing an error. 我写了这段代码并收到了这个错误。 我应该如何解决这个问题? - I wrote this code and received this error. How should I fix this? 在培训期间,我收到以下错误。 “ ValueError:没有为任何变量提供梯度” - Whie training I am getting the following error. “ ValueError: No gradients provided for any variable”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM