Python Forum
AttributeError: 'str' object has no attribute 'size'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AttributeError: 'str' object has no attribute 'size'
#1
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:
#   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 stdin
This 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.
Reply


Messages In This Thread
AttributeError: 'str' object has no attribute 'size' - by russoj5 - Nov-14-2020, 11:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Object has no attribute 'replaceall' ? peterp 2 7,265 Nov-10-2020, 09:23 PM
Last Post: buran
  Calling an class attribute via a separate attribute in input wiggles 7 2,977 Apr-04-2020, 10:54 PM
Last Post: wiggles
  AttributeError: 'list' object has no attribute 'g_s' NitinL 6 3,471 Mar-31-2020, 10:24 AM
Last Post: pyzyx3qwerty
  ERROR NoneType object has no attribute content denizkb 1 2,655 Nov-21-2019, 01:18 PM
Last Post: denizkb
  AttributeError: 'tuple' object has no attribute 'move' senfik99 2 4,079 Feb-26-2019, 12:42 PM
Last Post: stullis
  text = str(text.encode('utf-8')) AttributeError: 'float' object has no attribute 'enc ulrich48155 2 8,817 Jul-31-2017, 05:21 PM
Last Post: ulrich48155

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020