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