The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Snack, Vending Machine classes problem
Discuss Snack, Vending Machine classes problem in the Python Programming forum on Dev Shed. Snack, Vending Machine classes problem Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 4th, 2013, 12:13 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 5
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!
|

February 4th, 2013, 02:03 PM
|
 |
Contributing User
|
|
|
|
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!
|

February 4th, 2013, 09:18 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 5
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!
|

February 4th, 2013, 09:46 PM
|
 |
Contributing User
|
|
|
|
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')
|

February 19th, 2013, 07:43 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 15
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?
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|