121 Com: Week 8 Creating Classes

Screenshot - 21_11_2015 , 22_54_45
Figure 1.0
Screenshot - 21_11_2015 , 22_51_29
Figure 1.1
Screenshot - 21_11_2015 , 22_56_22
Figure 2.0
Screenshot - 21_11_2015 , 22_53_23
Figure 2.1
Screenshot - 21_11_2015 , 22_55_51
Figure 3.0

So this week in 121 Com, I gained an understanding of how to create my on classes as having only used the built in classes before hand. Classes use the the following syntax, of class followed by the new class name and and a colon. From here we can either enter pass within the code body and run in idle, of create functions within the class body. As you can see from figure 1.0, the task was to create a bank account where users could withdraw, transfer and deposit money, with the balance originally being set to zero. Figure 1.1 illustrates the script for this, so I created a class called Bank Account and used the __init__ function to create the class attributes and the parameters  including the parameter “self” that would later than have the arguments passed to them. I then created a withdraw, deposit and transfer function each with their own print statement. Figure 1.2 which is below, illustrates the print statements when the function is called. The clear issue with this is that if the user would remove £100 from zero they would be in negative equity, so to improve this I should have printed an else statement or raised an error to say this request is not valid. But buy depositing £200, the balance is back to a positive number.

Screenshot - 21_11_2015 , 23_18_54.png
Figure 1.2

Figure 2.0 which is the labsheet follows on from creating classes, requesting to create a a script similar to the one on the worksheet but instead the task is to create a class and function for fractions by use of a numerator and denominator. I also continued on with this idea of creating classes by creating my own calculator class with the following functions, divide, subtraction, multiplication and addition shown in figure 3.0.

Leave a comment