#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    15
    Rep Power
    0

    Help with a problem


    I am brande new to Python and I have an assignment for class and here are the directions:
    Name your source code file SweetTreats.py
    a) Implement the classes Snack and VendingMachine according to the specifications given below.
    b) Develop a main function to test each method in both classes.
    c) Document your test cases in comments below each class.

    Snack Class:
    Variables
    Description
    isSweet
    Boolean value, set to True if unspecified
    name
    The name of the snack, set to “ “ if unspecified
    price
    Holds the price of the snack, set to 1.00 if unspecified
    Constructor

    __init__(self, s, n, p)
    Constructs a snack object with isSweet, name, price set to the
    parameters s, n, p respectively.
    Methods

    __str__(self)
    Returns a string representation of the snack with its name, price,
    and isSweet values.

    VendingMachine Class:
    Variables
    Description
    snackList
    A list of the snacks in the vending machine
    location
    A string which represents the location of the vending machine, set
    to “NY” when unspecified.
    Constructor

    __init__(self, loc)
    Constructs a vending machine object containing an empty list of
    snacks and location set to the parameter loc.
    Methods

    addSnack(s)
    Add snack s to the list of snacks.
    getSaltySnack(p)
    Return any salty snack of cost at most p, if one exists on the list.
    Otherwise return None.
    getCheapestSweet()
    Return the cheapest sweet snack in the list. Return None if no
    sweet snack is available.
    getSweetCount()
    Return the number of sweet snacks currently in the list.
    display()
    Displays the current list of snacks in the vending machine.

    Here is my Snack Class, can anybody help me with the Vending Machine portion?

    Code:
    class Snack(object): 
     
        def __init__(self, n=' ', s=True, p=1.00): 
            self.isSweet = s 
            self.name = n      
            self.price = p
     
        def __str__(self):
            sweet = ('not ','',)[self.isSweet]
            return '%ssweet %s $%4.2f'%(sweet, self.name, self.price)
     
    def main():
    
        snack = Snack('Jolly Rancher',p=0.75)
        print(snack) #prints sweet Lifesavers $0.75
     
        snack = Snack('Reeses', False, p=2.25)
        print(snack) #prints not sweet Reeses $2.25
    
        snack = Snack('Skittles', p=1.50)
        print(snack)
     
     
    if '__main__' == __name__:
        main()
    Code:
    class vendingMachine(object):
        
        def __init__(self, loc="NY"):
            self.snackList = [self.name]
            self.location = loc
    
        def addSnack(snackList):
            snackList.append(s)
            print (self.snackList)
    
        def getSaltySnack(p):
    
        def getCheaperSweet():
    
        def getSweetCount():
    
        def display():
    Any help would be greatly appreciated!
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    15
    Rep Power
    0
    here is what i came up with so far. Can anybody help me in importing the snackList? How do i get it to print the list of snacks that i tested?

    Code:
     
    class Snack(object): 
     
        def __init__(self, n=' ', s=True, p=1.00): 
            self.isSweet = s 
            self.name = n      
            self.price = p
     
        def __str__(self):
            sweet = ('not ','',)[self.isSweet]
            return '%ssweet %s $%4.2f'%(sweet, self.name, self.price)
     
    
    
    snack = Snack('Jolly Rancher',p=0.75)
    print(snack) #prints sweet Lifesavers $0.75
     
    snack = Snack('Reeses', False, p=2.25)
    print(snack) #prints not sweet Reeses $2.25
    
    snack = Snack('Skittles', p=1.50)
    print(snack) #prints sweet Skittles $1.50
     
     
    
    class vendingMachine(object):
        
        def __init__(self, loc="NY"):
            self.snackList = []
            self.location = loc
    
        def addSnack(snackList):
            snackList.append(s)
            return (self.snackList)
    
        def getSaltySnack(p):
            if self.isSweet != None:
                #go thru list to find highest priced sweet snack
                return (self.isSweet)
            else:
                return None

IMN logo majestic logo threadwatch logo seochat tools logo