My Math Forum  

Go Back   My Math Forum > Science Forums > Computer Science

Computer Science Computer Science Forum

LinkBack Thread Tools Display Modes
November 3rd, 2015, 12:58 AM   #1
Joined: Oct 2015
From: earth

Posts: 5
Thanks: 0

Question regarding python programming, specifically Object Oriented Programming

Hi guys. I encountered this scenario online and I couldn't decipher what to do to make it work. I hope you could help me out..

So say for example you have a kiosk on your store and you want to program it for your customers. When a customer press number 1, he/she would be prompted to enter the product brand, model, price, and other specifics so that the system can store it and afterwards process it for the customer. However, not until the customer types in "-none", the system would continue asking him/her about the product. Also, there's a certain menu for the seller (the store) to look up the customer's entry by brand, price etc.

My question is, how should I tackle this? I think it's like putting input("Enter brand: ") into a certain variable and then attaching that variable to a class? However that would be complicated because there's also a menu for the seller wherein those things should be neatly grouped so the search can proceed. Sorry if it's kind of unclear but here's another analogy of it:


[1] Enter the product that you want:

[a] Brand:
[b] Price:
[c] Feature:

Do you want to add items to your cart? Y/N


Now that's for the customer. Here's for the seller:


[a] Search by brand
[b] Search by price
[c] Search by feature


So for example if I'm the customer and I enter "Skittles", and then I again enter "Snickers", it should be grouped neatly so when I use the seller mode, it can proceed smoothly too. I'm thinking of using a dictionary but we have 2 values: price and feature. How can I proceed with a specific search if it's grouped in a dictionary??

Sorry guys, total noob here.. Please help me.. Thank you so much...
mitch08 is offline  
November 5th, 2015, 06:43 AM   #2
Senior Member
Joined: Apr 2014
From: Glasgow

Posts: 2,142
Thanks: 726

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:

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:

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 06:57 AM.
Benit13 is offline  
November 28th, 2015, 05:51 AM   #3
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!
mitch08 is offline  

  My Math Forum > Science Forums > Computer Science

object, oriented, programming, python, question, specifically

Thread Tools
Display Modes

Similar Threads
Thread Thread Starter Forum Replies Last Post
Linear programming question cmc120 Linear Algebra 0 October 15th, 2014 08:04 AM
Object-Oriented Formal Specification - Help please GamerGirl Applied Math 0 May 4th, 2011 01:58 AM
Linear Programming question pls help xdeathcorex Linear Algebra 0 January 24th, 2011 08:08 AM
Object oriented and trees question Nivg Computer Science 0 May 3rd, 2010 05:21 PM
Possible programming base algebraic question. Please help? IsenseHelp Computer Science 3 March 23rd, 2009 10:47 PM

Copyright © 2019 My Math Forum. All rights reserved.