python 中的学生姓名和标记类 - 定义时出现未定义的错误

Student name and mark class in python - undefined error while it is defined

提问人:Dodee 提问时间:7/29/2020 最后编辑:ewokxDodee 更新时间:7/29/2020 访问量:421

问:

老实说,我不想粘贴这个,因为这是一个愚蠢的问题,但我尝试并多次更改代码。最后几行显示未定义对象的错误。你能帮忙吗?

    class Studentsmarks(object):
      def __init__(self, name, testmark):
        self.name=name
        self.testmark=testmark
      
      def getmark(self): #function to return mark attribute from the class object
        return self.testmark
      
      def getname(self): #function to access the class object and return the name attribute from it
        return self.getname
    
      def __str__(self): 
        return self.name + ' : ' + str(self.getname()) +'  ::'+  str(self.getmark())
    
    # Define a function for building a Record 
    # which generates list of all the students 
      def studnetobject(rec,name, testmark):
        rec.append(Studentsmarks(name, testmark))
        return rec
    
      def main():
        studentlist=[]
        for each in range(0,1):
          name=input("Enter student name:")
          testmark=input("Enter student mark:")
          studentlist=studnetobject(rec, name, testmark) # << ----------------error undefined studentsobject
        printlist(studentlist)
    
      def printlist(studentlist):
      # Printing the list of student
        print("\n") 
        print("\nList of Students\n") 
        for i in range(studentlist.__str__()):     
          rec.display(studentlist[i]) # <<-------------------------------Undefined rec
Python undefined-reference 函数调用

评论

0赞 ewokx 7/29/2020
您的缩进有点令人困惑。你打算在课堂上吗?main()studnetobjectprintlistStudentmarks
0赞 Dodee 7/29/2020
@ewong - 是的,我尝试了两种方法,当 main、studentobject 和 print list 进出类时 - 仍然有这些错误
0赞 ewokx 7/29/2020
请发布完整的回溯

答:

0赞 Induraj PR 7/29/2020 #1
class Studentsmarks(object):
  def __init__(self, name, testmark):
    self.name=name
    self.testmark=testmark
  
  def getmark(self): 
    return self.testmark
  
  def getname(self): 
    return self.name

def studnetobject(rec, name, mark):
  rec.append([name,mark])
  return rec

def main():
  studentlist = []
  for each in range(0,2):
    name=input("Enter student name:")
    testmark=input("Enter student mark:")
    student_obj = Studentsmarks(name=name,testmark=testmark)
    rec=[]
    studentlist.append(studnetobject(rec, student_obj.getname(), student_obj.getmark())) 
  return studentlist

def printlist(studentlist):
  print("\nList of Students and marks\n") 
  for i in range(len(studentlist)):     
    print(studentlist[i])

studentlist = main()
printlist(studentlist)

虽然这可以更简化,但为了帮助您通过比较来理解,我改编了您的代码。

enter image description here

评论

0赞 Dodee 7/29/2020
比你的贡献要多 - 我试过了,它有效。我认为我的错误是我没有正确传递对象属性。我的代码是:studentlist=studnetobject(rec, name, testmark) ,而你的更正是 studentlist.append(studnetobject(rec, student_obj.getname(), student_obj.getmark())) return studentlist
1赞 Schiz 7/29/2020 #2

我认为有几个错误。考虑到你的意图,我改变了它。

这是我写的代码......

class Studentsmarks(object):
    
    def __init__(self, name, testmark):
        self.name = name
        self.testmark = testmark

    def getmark(self): #function to return mark attribute from the class object
        return self.testmark

    def getname(self): #function to access the class object and return the name attribute from it
        return self.name

    def __str__(self): 
        return self.name + ' : ' + self.getmark()
    
# Define a function for building a Record 
# which generates list of all the students 
def studnetobject(rec, name, testmark):
    rec.append(Studentsmarks(name, testmark))
    return rec

def main():
    studentlist=[]
    for i in range(0, 2): # 2 inputs (You can change here)
        name = raw_input("Enter student name:")
        testmark = raw_input("Enter student mark:")
        studentlist = studnetobject(studentlist, name, testmark)
    printlist(studentlist)

def printlist(studentlist):
    # Printing the list of student
    print("\n") 
    print("\nList of Students\n") 
    for i in range(len(studentlist)):
        print(studentlist[i])

if __name__ == '__main__':
    main()
  1. “raw_input”是更合适的功能。“raw_input”仅返回字符串。

  2. 更改了“str”功能。它打印名称和标记。

你还会发现其他东西。我希望这段代码对你有所帮助。

评论

0赞 Dodee 7/29/2020
非常感谢您对@Schiz的帮助 - 我尝试了您的代码并且有效。你能解释一下最后一行是做什么的吗?if name == 'main': main() 只是为了学习
0赞 Schiz 7/30/2020
medium.com/python-features/......
0赞 Herlangga Yusuf 7/29/2020 #3
class Studentsmarks(object):
    def __init__(self, name, testmark):
        self.name=name
        self.testmark=testmark
    
    def getmark(self): #function to return mark attribute from the class object
        return self.testmark

    def getname(self): #function to access the class object and return the name attribute from it
        return self.name

    def __str__(self): 
        return self.name + ' : ' + str(self.getname()) +'  ::'+  str(self.getmark())
    
    # Define a function for building a Record 
    # which generates list of all the students 
def studnetobject(name, testmark):
    return Studentsmarks(name, testmark)

def main():
    studentlist=[]
    for each in range(0,3):
        name=input("Enter student name:")
        testmark=input("Enter student mark:")
        studentlist.append(studnetobject( name, testmark)) # << ----------------error undefined studentsobject
    printlist(studentlist)

def printlist(studentlist):
# Printing the list of student
    print("\n") 
    print("\nList of Students\n") 
    print(*studentlist, sep='\n')
main()

我不知道您期望什么输出,但我认为它在 Object 中是一个很好的实现。