Python Forum

Full Version: Generating a student's transcript [OOP concept]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

This is part of my homework. We're trying to learn/apply some concepts of OOP in this exercise.

This is what I've got so far.
def welcome(self):
          print("==========WELCOME==========")

class demographics:
          def __init__(self,my_id,age,gender,location,*args, **kwargs):
                    #super(demographics,self).__init__(*args, **kwargs)
                    welcome(self)
                    print("")
                    #print(my_id)
                    #print(age)
                    #print(gender)
                    self.location = str(input("Are you In-State Y/N: "))
                    if self.location=='Y' or self.location=='y':
                              print("The In-State School Fee is $560.")
                    else:
                              print("The Out-of-State School Fee is $913.")
                                  
class grade:
          def __init__(self,math,science,english, *args, **kwargs):
                    super(grade,self).__init__(*args, **kwargs)
                    ave=(math+science+english)/3
                    if ave > 90:
                              print("Your final grade is A")
                    if ave >80 and ave <=90:
                              print("Your final grade is B")
                    if ave >70 and ave <=80:
                              print("Your grade is C")
                    if ave <=70:
                              print("Your grade is F")

class final(demographics,grade):
          def __init__(self,*args, **kwargs):
                    super(final,self).__init__(*args, **kwargs)
                    demographics(self,my_id,gender,location)
                    print("====Student's Transcript Report====")
                    print("Student ID: ",my_id,"","Age: ",age,"","Location (In-State Y/N): ",location)
                    print("")
                    print(math)
                    print(science)
                    print(english)
                    print("Your average in three subjects is ",ave)
However when I run my code using this example:

x=final(my_id="001",age="33",gender="M",location="Y",math=95,science=100,english=95). I encountered a "NameError" where a variable/variables are not defined. See output below:

Output:
>>> x=final(my_id="001",age="33",gender="M",location="Y",math=95,science=100,english=95) ==========WELCOME========== Are you In-State Y/N: y The In-State School Fee is $560. Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> x=final(my_id="001",age="33",gender="M",location="Y",math=95,science=100,english=95) File "C:\Users\among\Desktop\Assignment 3 Python\Assignment 3 Part 3.py", line 36, in __init__ demographics(self,my_id,gender,location) NameError: name 'my_id' is not defined >>>
Thanks very much. Appreciate any constructive feedback.

Best,
Art
Capturing optional keyword args via (**kwargs) doesn't place the args into the function's namespace.

Example:
def myfunc(**kwargs):
    print(kwargs)
    print(kwargs["key"]) #okay
    print(key)           #not okay, even if passed in as an argument

myfunc(key="value")
During this call, you can get the passed-in arguments by examining kwargs. But you cannot query the local variable "key". That would have to be assigned within the function.
It looks like you misunderstand OOP concept. You just wrap some functions disguised as __init__() method of a "class" and then call them by instantiating the class while throwing away the created instance.
Also you pass some arguments, like location in the __init__ method of demographics, but don't use it at all.
I would suggest you rethink your approach.
Class is a blue-print of an object you want to create [multiple] instances of. For example a Student class would be a good example. Then it may have different methods and properties (i.e. attributes)