Python代码编译问题
请帮忙。我是一名python初学者,我正在使用类尝试这个简单的排序程序,但我一直遇到以下错误Python代码编译问题,python,Python,请帮忙。我是一名python初学者,我正在使用类尝试这个简单的排序程序,但我一直遇到以下错误 AttributeError:“str”对象没有属性“mark”listofStds是字符串列表,而不是student实例列表。您正在尝试按属性标记对其排序,该属性对于字符串不存在 您可能希望有一个学生列表,因此您需要: class student: def __init__(self, name, age, mark): self.name = name sel
AttributeError:“str”对象没有属性“mark”
listofStds
是字符串列表,而不是student
实例列表。您正在尝试按属性标记对其排序,该属性对于字符串不存在
您可能希望有一个学生列表
,因此您需要:
class student:
def __init__(self, name, age, mark):
self.name = name
self.age = age
self.mark = mark
std1 = student("name1", 14, 45)
std2 = student("name2", 13, 90)
std3 = student("name3", 14, 70)
std4 = student("name4", 14, 80)
std5 = student("name5", 13, 75)
listofStds = ["std1", "std2", "std3", "std4", "std5"]
for x in sorted(listofStds,key=lambda x: x.mark):
print x
listofStds
是字符串列表,而不是student
实例列表。您正在尝试按属性标记对其排序,该属性对于字符串不存在
您可能希望有一个学生列表
,因此您需要:
class student:
def __init__(self, name, age, mark):
self.name = name
self.age = age
self.mark = mark
std1 = student("name1", 14, 45)
std2 = student("name2", 13, 90)
std3 = student("name3", 14, 70)
std4 = student("name4", 14, 80)
std5 = student("name5", 13, 75)
listofStds = ["std1", "std2", "std3", "std4", "std5"]
for x in sorted(listofStds,key=lambda x: x.mark):
print x
请确保在发布错误消息时发布完整堆栈跟踪(以“Traceback”开头的部分)。ListOfsDs=[std1、std2、std3、std4、std5]删除所有“”,因为您需要对象请确保在发布错误消息时发布完整堆栈跟踪(以“Traceback”开头的部分)。ListOfsDs=[std1,std2,std3,std4,std5]删除所有“”,因为您需要对象请告诉我如何修复它。我是一个完全的初学者。@user2655793,Zdeslav Vojkovic的答案包含修复。@user2655793更改行列表OfstDS,就像Zdeslav Vojkovic的答案所述。我通过修复获得以下输出:@user2655793:“std1”
是一个字符串,其中包含文本std1
。std1
是一个引用。您需要引用。请告诉我如何修复它。我是一个完全的初学者。@user2655793,Zdeslav Vojkovic的答案包含修复。@user2655793更改行列表ofsts,就像Zdeslav Vojkovic的答案所述一样。我用f得到以下输出ix:@user2655793:“std1”
是一个字符串,其中包含文本std1
。std1
是一个引用。您需要引用。