Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class and calling a method
#1

class student:
    
    def __init__(self, first_name, last_name, student_id, year):
        self.first_name = first_name
        self.last_name = last_name
        self.student_id = student_id
        self.year = year
        self.course = []
        
    def __str__(self):
        return self.first_name, self.course
        
    def enroll(self, course):
        self.course = course
    


def main():
    
    file = student('John', 'Smith', 'L01234567', 'senior')
    print(file)
    file.enroll ('CSC225')
    


    
main()
so when I call enroll on 'CSC225' I want it to print “John is enrolled in CSC225.” But I cannot figure out what I am doing wrong. I just threw in the print statement to test it out but it is not working the way I thought it should. This is the latest error I have been getting when with what I have last tried to get it to work:
Error:
line 22, in main print(file) builtins.TypeError: __str__ returned non-string (type tuple)
Quote
#2
You need to construct the string in the __str__ method if that is what you want it to look like.
Also you need to decide if Student.course is a list or a single item.

class Student(object):
    def __init__(self, first_name, last_name, student_id, year):
        self.first_name = first_name
        self.last_name = last_name
        self.student_id = student_id
        self.year = year
        self.course = []
         
    def __str__(self):
        return "{} is enrolled in {}.".format(self.first_name, self.course)
         
    def enroll(self, course):
        self.course.append(course)
     
 
def main():
    student = Student('John', 'Smith', 'L01234567', 'senior')
    student.enroll('CSC225')
    print(student)
     

main()
buran likes this post
Quote
#3
Did you get this figured out, or are you still needing help?
Quote
#4
I got it thanks!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Calling function-- how to call simply return value, not whole process juliabrushett 2 108 Jul-01-2018, 01:17 AM
Last Post: juliabrushett
  seek method clarification petepiro 0 143 Jan-17-2018, 01:23 AM
Last Post: petepiro
  Method on instance changing its representation Samsar 1 268 Dec-20-2017, 07:39 AM
Last Post: Terafy
  Iteration Inverse Method antoniomancera 7 498 Nov-24-2017, 09:52 AM
Last Post: heiner55
  Method of k-nearest neighbors qwerty 1 501 May-07-2017, 06:42 PM
Last Post: buran
  Classification by support vector method qwerty 1 587 Apr-16-2017, 07:16 PM
Last Post: sparkz_alot
  Python classes and init method Alaweey98 9 1,424 Apr-03-2017, 12:02 AM
Last Post: wavic
  Calling a Returned Value to Another Function valerydolce 9 1,297 Mar-28-2017, 09:54 PM
Last Post: valerydolce
  Calling a function to return a list of percentages Liquid_Ocelot 7 1,091 Mar-25-2017, 01:20 PM
Last Post: Larz60+
  Need help on scikitLearn StratifiedKFold method debupython 0 1,398 Nov-28-2016, 08:43 AM
Last Post: debupython

Forum Jump:


Users browsing this thread: 1 Guest(s)