Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 4th, 2013, 12:13 PM
rbmlmc rbmlmc is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 5 rbmlmc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 11 sec
Reputation Power: 0
Snack, Vending Machine classes problem

Hello, all. I am new to programming (second semester) and it is not my strong suit. 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 attempt at creating the snack class, any ideas as to what's wrong:
class Snack(object):

def __init__(self, s, n, p):
self.isSweet = s
self.name = n.str()
self.price = p

def isSweet(self, s):
if self.isSweet != None:
return self.isSweet
else:
self.isSweet = True
return self.isSweet

def getName(self, n):
if self.name != None:
return self.name
else:
self.name = ""
return self.name

def getPrice(self, p):
if self.price != None:
return self.price
else:
self.price = 1.00
return self.price

def __str__(self):
desc = str.("Snack: ", self.name, "costs ", self.price, "and is it sweet?", self.isSweet)
return desc

Thank you for the help, it is greatly needed and even more-so, appreciated!

Reply With Quote
  #2  
Old February 4th, 2013, 02:03 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,367 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 11 h 16 m 5 sec
Reputation Power: 383
Here's a better start for your Snack class. It needs more (one or two more) tests than I've provided. To preserve critical whitespace please use code tags when posting python. See link at my signature.
Code:
'''
    The doctest is more effective than running the main
    routine for testing since the computer will tirelessly
    verify the result each time you run the test.
    This is a bash command to run doctest on the file
    /tmp/p.py :

    $ ( cd /tmp && python -m doctest -v p.py )
'''

class Snack(object):

    '''
        >>> # a doctest
        >>> str(Snack('Livesafers',p=0.32))
        'sweet Livesafers $0.32'
    '''

    def __init__(self, n=' ', s=True, p=1.00):   # use default arguments
        self.isSweet = not not s   # ensure isSweet is Boolean
        self.name = n          # n.str() Why would you think the n argument has an str method?
        self.price = p

    def __str__(self):
        sweet = ('not ','',)[self.isSweet]
        return '%ssweet %s $%4.2f'%(sweet, self.name, self.price)

def main():
    '''
        test all classes and methods
    '''
    snack = Snack('Livesafers',p=0.32)
    print(snack)

if '__main__' == __name__:
    main()
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #3  
Old February 4th, 2013, 09:18 PM
rbmlmc rbmlmc is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 5 rbmlmc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 11 sec
Reputation Power: 0
Thank you very much! I have a couple questions, though: Could you explain what's going on in the self.isSweet step? What does it mean that self.isSweet = not not s? Also, could you explain the last step in the __str__ method? I'm a little unsure about what all of those symbols mean...Thank you so much for the insight!

Reply With Quote
  #4  
Old February 4th, 2013, 09:46 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,367 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 11 h 16 m 5 sec
Reputation Power: 383
You can experiment in the python interpreter.
All python objects are either true or false.
Inputs are often bad. not not Object is the truth value of an object converted to Boolean.
Code:
>>> (True, not True , not not True)
(True, False, True)
>>> ('a' , not 'a', not not 'a')
('a', False, True)
>>> ('', not '', not not '')
('', True, False)
>>> 
Since strings of length 0 are false and all other strings are true you see that what I've done is misleading.
(not not 'False') is True
To test isSweet I should use
isSweet in (False,True)
and raise an exception for bad data.

Your other question involves string formatting. The new style would read
'{}sweet {}, ${:.2f}'.format('not ','potaco thips',0.64)
Look up the % operator for strings. Strange, I can't find it either. Look here. You'll need to know
('I','am','a','tuple')

Reply With Quote
  #5  
Old February 19th, 2013, 07:43 PM
PythonNewb PythonNewb is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 15 PythonNewb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 54 m 19 sec
Reputation Power: 0
Quote:
Originally Posted by rbmlmc
Thank you very much! I have a couple questions, though: Could you explain what's going on in the self.isSweet step? What does it mean that self.isSweet = not not s? Also, could you explain the last step in the __str__ method? I'm a little unsure about what all of those symbols mean...Thank you so much for the insight!


Hey, did you ever figure out the vending machine portion and would you mind posting it?

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Snack, Vending Machine classes problem

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap