Reputation: 107
I'm really confused about how classes work, I've written this code but get the error message
AttributeError: 'str' object has no attribute 'pLatin_converter'
but surely the object does have that attribute since it is of the class pigLatin_class
?
class pigLatin_class (object):
'class for converting plain text into pig latin'
vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
def __init__ (string):
pig = ""
movetoend = ""
index = 0
def pLatin_converter (string):
listeng = string.split()
for word in listeng:
length = len(word)
movetoend = ""
index = 0
if word[0] not in vowels:
for l in word:
if l not in vowels:
movetoend = movetoend + l
index = index + 1
else:
break
pig = pig + " " + word[index:length] + movetoend + "ay"
elif word[0] in vowels:
pig = pig + " " + word[1:length] + "hay"
print("pig latin is: " + pig)
words = pigLatin_class()
words = "Hi I'm Amy"
words.pLatin_converter()
edit: okay i see my initial mistake, now if I input
words = pigLatin_class("Hi I'm Amy")
words.pLatin_converter()
it says I've given 2 positional arguments?
Upvotes: 0
Views: 104
Reputation: 365707
surely the object does have that attribute since it is of the class "pigLatin_class"?
But it's not. You do create an instance of pigLatin_class
, but then you immediately replace it with an instance of str
:
words = pigLatin_class()
words = "Hi I'm Amy"
And str
isn't pigLatin_class
, and doesn't have a pLatin_converter
method.
I think you wanted something like this:
words = pigLatin_class("Hi I'm Amy")
words.pLatin_converter()
But then you need to fix a few more bugs. Most crucially, the __init__
method of your class has to take a self
parameter, as well as a string
parameter, and then it needs to store things—including that string parameter value—on that self
to be used later:
def __init__(self, string):
self.string = string
self.pig = ""
self.movetoend = ""
self.index = 0
And then, your methods will also take self
and can use those attributes:
def pLatin_converter(self):
listeng = self.string.split()
# etc.
You may have instead wanted something like this:
words = pigLatin_class()
words.pLatin_converter("Hi I'm Amy")
In that case, you won't be taking string
as a second parameter in __init__
and storing it in self.string
, you'll instead be taking it as a second parameter in pLatin_converter
. Hopefully you know enough now to change it to work that way, and to understand the difference.
Upvotes: 3
Reputation: 969
You are passing the string 'Hi I'm Amy' to the pLatin_converter() method.
Instead, you need to do this:
class pigLatin_class (object):
'class for converting plain text into pig latin'
def __init__ (self, string):
self.string = string
self.vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
def pLatin_converter(self):
pig = ""
movetoend = ""
index = 0
listeng = self.string.split()
for word in listeng:
length = len(word)
movetoend = ""
index = 0
if word[0] not in self.vowels:
for l in word:
if l not in self.vowels:
movetoend = movetoend + l
index = index + 1
else:
break
pig = pig + " " + word[index:length] + movetoend + "ay"
elif word[0] in self.vowels:
pig = pig + " " + word[1:length] + "hay"
print("pig latin is: " + pig)
words = pigLatin_class("Hi I'm Amy")
words.pLatin_converter()
You need to define variables that are class based with self.var_name and then you can access them throughout the class, by passing self as a method argument. In this case you pass the string to init() and save it in self.string variable, then you can access it when pLatin_converter() method is called.
Upvotes: 1