Python Forum
Generating a student's transcript [OOP concept]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Generating a student's transcript [OOP concept]
#1
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
Reply
#2
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.
Reply
#3
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)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Student project - alert action when X happens Y amt of times, how? unknown00 2 1,781 Aug-25-2021, 08:07 PM
Last Post: namarang
  Student grader and sorter for assignment RazeAD 7 3,142 Feb-11-2021, 06:29 AM
Last Post: RazeAD
  Create code for input names and score for 15 student Davin 2 2,036 Sep-21-2020, 08:49 AM
Last Post: DeaD_EyE
  New Python Student = Does this code look right? musicjoeyoung 6 3,363 May-07-2020, 02:39 PM
Last Post: musicjoeyoung
  Understanding the "self" concept peace 7 3,283 Feb-10-2020, 08:21 PM
Last Post: nilamo
  Help learning a concept Ccjake 2 2,286 Jan-22-2019, 01:35 AM
Last Post: Ccjake
  Student grade program help debug ccm1776 3 5,095 Nov-14-2018, 02:41 AM
Last Post: stullis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020