My Math Forum Question regarding python programming, specifically Object Oriented Programming

 Computer Science Computer Science Forum

 November 5th, 2015, 07:43 AM #2 Senior Member   Joined: Apr 2014 From: Glasgow Posts: 2,133 Thanks: 719 Math Focus: Physics, mathematical modelling, numerical and computational solutions When designing a software solution for any problem, start writing pseudocode. Pseudocode is the name for line-by-line instructions that you want to happen without actually typing syntactically correct code. It's sort of a basic outline of how you think things should proceed. For example, you might want the program to operate something like this: 1. Start 2. Wait for a user to enter a number 3. If the number is 1 - 3.1 Print "Please enter a brand" - 3.2 Wait for user to enter a brand - 3.3 Print "Please enter a Price" - 3.4 Wait for user to enter a price - 3.5 Print "Please enter a Feature" - 3.6 Wait for user to enter a Feature - 3.7 Go back to step 2. 4. If the number is 0 - 4.1 quit from the program Once you have a good idea of how things are going to look, start going into more detail. Do you want to check each user input to make sure there are no errors? Once a user has entered an item of input, where is that item going to be stored? Some hints: Object-orientated programming in Python is fairly easy. Here's an example of a simple object called StoreItem: Code: class StoreItem: def __init__(self): self.brand = "" self.model = "" self.price = 0.0 def Discount(self, percentageDiscount): return self.price*(1.0 - percentageDiscount/100.0) "class" is used to declare the object. "StoreItem" is now used as the name of the object. Each object you can have multiple instances. "def" is used to define functions that take input of some kind and spits out output. In the above case the function being defined is a special one called "__init__(self)". This is a special reserved function which is called whenever an instance for this object is being made. It is an initialiser. The "self" keyword is also very important, because this initialiser function needs access to the object's member variables. That's what the "self" bit means. self.brand, self.model and self.price are the member variables. You can define whatever ones you want. The initialiser should set values for the various fields to start with. Since you don't know what item you have to being with, you can just set the brand and model to be empty strings and the price to zero. You can then later update these as necessary. There is a second function called "Discount()". This is not a special name like __init__() so it will only operate if you manually call it in the code. I added it just as an example It gives back to the user a discounted price based on the percentage discount you want to apply Here's an example of the object in use: Code: snickers = StoreItem() snickers.brand = "Mars" snickers.model = "Snickers" snickers.price = 0.80 sneakers = StoreItem() sneakers.brand = "Converse" sneakers.model = "All Stars" sneakers.price = 45.0 newPrice = sneakers.Discount(20.0) print newPrice It's up to you how you want to manage your database of StoreItems. You could have a list, a dictionary, even another class. It's up to you. Lists are assigned and accessed using an index (an integer). E.g. myList[0] = snickers sets the first item in the list to be the snickers bar instance. myList[0] gives the first item in the list (gives me back the snickers bar). myList[12] gives the 13th item in the list, myList[-1] gives the last item in the list. Note: I usually define empty lists (newList = []) and then append items to them (newList.append(value)) when I'm first defining the list entries. Only once I know I've finished adding items to the list do I start accessing them using indices. Dictionaries are assigned and accessed using a key (a string). E.g. myList["Snickers"] = snickers would add to the dictionary the snickers object, which is accessed using the key "Snickers". Last edited by Benit13; November 5th, 2015 at 07:57 AM.
 November 28th, 2015, 06:51 AM #3 Newbie   Joined: Oct 2015 From: earth Posts: 5 Thanks: 0 @Benit13 Thank you so much for the help! Sorry I wasn't able to thank you right away since I had to finish some tasks! But I really appreciate it! Thanks again!

 Tags object, oriented, programming, python, question, specifically

 Thread Tools Display Modes Linear Mode

 Similar Threads Thread Thread Starter Forum Replies Last Post cmc120 Linear Algebra 0 October 15th, 2014 09:04 AM GamerGirl Applied Math 0 May 4th, 2011 02:58 AM xdeathcorex Linear Algebra 0 January 24th, 2011 09:08 AM Nivg Computer Science 0 May 3rd, 2010 06:21 PM IsenseHelp Computer Science 3 March 23rd, 2009 11:47 PM

 Contact - Home - Forums - Cryptocurrency Forum - Top