Changes

Jump to: navigation, search

OPS435 Python Lab 6

34 bytes removed, 12:00, 29 November 2017
no edit summary
== PART 1 - Creating a Class ==
:The class that is written in Python can contain variables, functions, code, but none of the code is executed or run until the class is used to create the object. Remember that the class is a blueprint for how your object will work, the object that will be created is what will actually be running the code. This part of a class works in the same way as a function definition, the function doesn't run until it's executed, code inside classes don't run until the are made into objects.
:'''Perform the Following Steps:'''
print('GPA of student ' + self.name + ' is ' + str(gpa / len(self.courses)))
</source>
:# Now that the class has been broken down, the code inside is simple on it's own, but what does it mean when it all works together? Before you can use a class, we must create a new object. A single class description definition can be used to create as many objects as you like, not just a single object. Our '''Student''' class will be used to create lots of different student objects, each object will be created using the blueprint of the class, but they will all be separate and contain separate data to store each and every student's data. Try to think of an object as any other python data structure (list, set, dictionary): you can create multiple dictionaries and store difference different data inside, even if some of the data is the same.
:# Import the python script into your ipython environment:<source lang="python">
from student import Student
# Creates an instance of the Student class, it will be separate from all other objects created with the Student class:
student1 = Student('John', '013454900')
student1.addGrade('uli101', 4.0)
student1.addGrade('ops235', 3.5)
student1.addGrade('ops435', 3.0)
</source>
:# Now that the object '''student1''' has been created, look closely at the components inside the object. This is a look into the objects '''namespace''':<source lang="python">
:# Before going further with '''student1''', lets create a second object, to demonstrate that these are different data structures:<source lang="python">
student2 = Student('Jessica', '023384103')
</source>
:# Take a closer look at some of these different attributes and methods.<source lang="python">
student2.name
student2.number
# Add new courses for student2
student2 = Student('Jessica', '023384103')
student2.addGrade('ipc144', 4.0)
student2.addGrade('cpp244', 4.0)
=== Create a Python Script Demonstrating Classes ===
:# The following python script is broken. It has two major problems to fix and one new feature to add: first problem is providing the student number as an integer causes an error(TypeError) when displayStudent() is run, second problem is in displayGPA() may divide by zero error(ZeroDivisionError) if no courses are added to the dictionary or the grades added to the dictionary are 0.0 floats. Finally, you will add a new method to this class that prints out a formatted list of all courses the student has taken.
:#Create the '''~/ops435/lab6/lab6a.py''' script.
:#Use the following as a template(warning this is NOT the same as student.py):<source lang="python">
198
edits

Navigation menu