I have to make a program that converts a string to pig latin and include an constructor + example of the program. I then have to make a subclass based on it with a overwritten method to add a few new rules.
In the original class file I have the constructor:
pig = pigLatin_Class("First test")
print(pig)
pig.sentence = "Changing it up"
print(pig)
Which works correctly, however in my new subclass:
gib = gibberish_class("test")
print(gib)
Which also works correctly but prints out the answers from the super class as well, even though they're outside of the class definition.
I'm using:
import assignment_a3_1 as a3_1
class gibberish_class(a3_1.pigLatin_Class):
as my inheritance.
Any solutions?
EDIT:
In regards to the naming conventions, I'm just using what we were told to use, also I've only been coding for a month or two at most, doing a uni course, be gentle! :D
Super:
class pigLatin_Class(object):
vowels = ["a","e","i","o","u","A","E","I","O","U"]
def __init__(self, initialSentence):
self.__sentence = initialSentence
self.sentencePig = []
self.pLatin_converter(self.__sentence)
def pLatin_converter(self, sentence):
wordList = sentence.split(" ")
for word in wordList:
isVowel = False
wList = list(word)
for character in word: #vowel checks
if(character in pigLatin_Class.vowels):
isVowel = True
break
if(isVowel == False): #finishing if no vowels
wList.append(wList.pop(0))
word = ''.join(wList) + "way "
self.sentencePig.append(word)
if(''.join(wList) == word): #Stops it from iterating through again if it contains no vowels
if(wList[0] in pigLatin_Class.vowels):
word = ''.join(wList) + "hay "
self.sentencePig.append(word)
elif(wList[0] not in pigLatin_Class.vowels): #contains vowels but not first
for i in range(len(wList)):
if(wList[i] in pigLatin_Class.vowels):
wList = wList[i:] + wList[:i]
word = ''.join(wList) + "ay "
self.sentencePig.append(word)
break
def __str__(self):
string = ("\n The translation of '" + self.__sentence + "' is: " + ''.join(self.sentencePig))
return string
@property
def sentence(self):
return self.__sentence
@sentence.setter
def sentence(self, value):
self.__sentence = value
self.sentencePig = []
self.pLatin_converter(self.__sentence)
pig = pigLatin_Class("First test")
print(pig)
pig.sentence = "Changing it up"
print(pig)
Subclass:
import assignment_a3_1 as a3_1
class gibberish_class(a3_1.pigLatin_Class):
symb = ["0","1","2","3","4","5","6","7","8","9",",",".","/",";","#","[","]","=","-","(",")","*","&","^","%","$","£","_","+","{","}","~","<","@",":","?",">","<","|"]
vowels = a3_1.pigLatin_Class.vowels
def pLatin_converter(self, sentence):
wordList = sentence.split(" ")
for word in wordList:
suffix = 0
isVowel = False
wList = list(word)
for character in word: #vowel checks
if(character in gibberish_class.vowels):
isVowel = True
break
if(isVowel == False): #finishing if no vowels
wList.append(wList.pop(0))
suffix = 1
if(''.join(wList) == word): #Stops it from iterating through again if it contains no vowels
if(wList[0] in gibberish_class.vowels):
suffix = 2
elif(wList[0] not in gibberish_class.vowels): #contains vowels but not first
for i in range(len(wList)):
if(wList[i] in gibberish_class.vowels):
wList = wList[i:] + wList[:i]
suffix = 3
break
for character in wList:
#print(character)
if(character in gibberish_class.symb):
wList.append(character)
if(suffix == 1):
word = ''.join(wList) + "way "
elif(suffix == 2):
word = ''.join(wList) + "hay "
elif(suffix == 3):
word = ''.join(wList) + "ay "
self.sentencePig.append(word)
gib = gibberish_class("test")
gib.pLatin_converter
print(gib)
EDIT TWO:
The another question was very helpful in stopping the main method, however the str method is still called in the first question, just printing an empty string, said if statement just causes the program to throw errors if used in the str method. How would I solve that?