Nov-14-2020, 11:44 PM
Hi All,
I'm doing a homework assignment and getting some errors when running the doctest. Everything seems to work fine when I manually enter data, but when I run the doctest, I am getting the same 3 errors.
This is the code that I have written for the homework assignment:
Any suggestions on what I should look at or hints to get me moving in the right direction would be very helpful and appreciated.
I'm doing a homework assignment and getting some errors when running the doctest. Everything seems to work fine when I manually enter data, but when I run the doctest, I am getting the same 3 errors.
This is the code that I have written for the homework assignment:
# Pizza() # **** COMPOSITION ****** class Pizza(): #__init__ *****MODIFY METHOD****** def __init__(self, s='M', t={}): #print('__init__') self.size = s self.toppings = set(t) #__repr__ *****RETURN METHOD****** def __repr__(self): return f"Pizza('{self.size}',{self.toppings})" #__eq__ ******RETURN METHOD***** def __eq__(self,other): return self.size == other.size and self.toppings == other.toppings #__setSize__ ****MODIFY METHOD****** def setSize(self,s): self.size = s #__getSize__ *****RETURN METHOD******* def getSize(self): return self.size #__addTopping__ ******MODIFY METHOD***** def addTopping(self,t): #Error - str does not have "add" #why is it being passed a string?? self.toppings.add(t) #__removeTopping__*****MODIFY METHOD******* def removeTopping(self, t): self.toppings.discard(t) #__price__ ****** RETURN METHOD ****** def price(self): costDict={'S':0.70,'M':1.45,'L':1.85} if self.size == 'S': return 6.25 + costDict.get(self.size) * len(self.toppings) elif self.size == 'M': return 9.95 + costDict.get(self.size) * len(self.toppings) else: return 12.95 + costDict.get(self.size) * len(self.toppings) def orderPizza(): print('Welcome to Python Pizza!') #print(input('What size pizza would you like (S,M,L): '))# check that it receives input size = input('What size pizza would you like (S,M,L): ') #print(p.setSize) p=Pizza(size,t={}) while True: newTopping = input('Type of topping to add (or Enter quit): ') #print(p) if newTopping == '': break else: p.addTopping(newTopping) #print(p) print('Thanks for ordering!') return f"Your pizza costs ${p.price()})" if __name__=='__main__': import doctest print( doctest.testfile( 'hw8TEST.py'))This is the doctest code that is running and causing the errors:
##### orderPizza ##### >>> orderPizza()==Pizza('M',{'garlic', 'onion', 'mushroom'}) Welcome to Python Pizza! What size pizza would you like (S,M,L): Type topping to add (or Enter to quit): Type topping to add (or Enter to quit): Type topping to add (or Enter to quit): Type topping to add (or Enter to quit): Thanks for ordering! Your pizza costs $14.299999999999999 True >>> orderPizza()==Pizza('L',{'calamari', 'garlic'}) Welcome to Python Pizza! What size pizza would you like (S,M,L): Type topping to add (or Enter to quit): Type topping to add (or Enter to quit): Type topping to add (or Enter to quit): Thanks for ordering! Your pizza costs $16.65 True >>> orderPizza()==Pizza('S',set()) Welcome to Python Pizza! What size pizza would you like (S,M,L): Type topping to add (or Enter to quit): Thanks for ordering! Your pizza costs $6.25 True #stdin back # put stdin back to original, again, shouldnt cause error >>> sys.stdin = si # return stdinThis is the typical error:
Output:File "/Users/jamesrusso/Desktop/DePaul/CSC 401 Introduction to Programming/Week 8/hw8TEST.py", line 77, in hw8TEST.py
Failed example:
orderPizza()==Pizza('M',{'garlic', 'onion', 'mushroom'})
Exception raised:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/doctest.py", line 1336, in __run
exec(compile(example.source, filename, "single",
File "<doctest hw8TEST.py[26]>", line 1, in <module>
orderPizza()==Pizza('M',{'garlic', 'onion', 'mushroom'})
File "/Users/jamesrusso/Desktop/DePaul/CSC 401 Introduction to Programming/Week 8/hw8.py", line 32, in __eq__
return self.size == other.size and self.toppings == other.toppings
AttributeError: 'str' object has no attribute 'size'
It looks like it doesnt like the __eq__ function in the Pizza() class. I've been playing around with it for a few hours and not sure what I'm doing wrong. I'm at the point where I've been guessing at different solutions without any real reason for trying them and feel like I don't have a good enough understanding of the material at this point because I'm stuck. Any suggestions on what I should look at or hints to get me moving in the right direction would be very helpful and appreciated.