I need some help with creating a python class and method. I don't really know what I am doing wrong but I keep getting the correct answer and then this error:
<__main__.stringToMerge object at 0x7f9161925fd0>
I want to create an object with two string that merges them alternatively. For example the object would be obj.s1="aaaaa", obj.s2="bb" and the correct output would be: "ababaaa".
Ty in advance for any help provided :D
class stringToMerge:
def __init__(self, string1, string2):
self.string1 = string1
self.string2 = string2
def SM(self, string1, string2):
self.string1 = string1
self.string2 = string2
string3 = ""
i = 0
while i<len(string1) and i<len(string2):
string3 = string3+string1[i]
string3 = string3+string2[i]
i = i+1
while i<len(string1):
string3 = string3+string1[i]
i = i+1
while i<len(string2):
string3 = string3+string1[i]
i = i+1
print(string3)
obj = stringToMerge('aaaaa', 'bb')
obj.SM(obj.string1, obj.string2)
print(obj)
SMmethod? And if so, does it actually need to take arguments? You're not benefiting from the state stored in the initializer here. Seems like all you really need is a__init__and a__repr__or__str__, noSMat all. Are you allowed to use theitertoolsmodule? If so, theroundrobinrecipe would do your merging much more cleanly (''.join(roundrobin(self.string1, self.string2))does it all).printan object. You should provide a__repr__and a__str__method in your class. docs.python.org/3.4/reference/datamodel.html#object.__repr____init__) should probably just be a function.obj.SM(); the method doesn't need arguments other thanself, and you can definestring1 = self.string1; string2 = self.string2inside the body of the method.