'the method b. is_palindrom() does not work

Could you please help me why the second method does not work in the class even though it works separately?

the question is

Develop a SuperStr class that inherits the functionality of the standard str type and contains 2 new methods: a. is_repeatance(s) method, which takes 1 argument s and returns True or False depending on whether the current row can be obtained by an integer number of repetitions of the string s. Return False if s is not a string. Assume that the empty string contains no repetitions. b. is_palindrom() method, which returns True or False depending on whether the string is a palindrome. Ignore the case of characters. The empty string is considered a palindrome.

my code:

class SuperStr:
    def __init__(self, main_str):
        self.main_str= main_str
  
    def is_repeatance(self, s):
        string = self.main_str
        count = 0
        is_repeated = False
        if string == '':
            return False
        else:
            for i in range(0, len(string)):
                if string[i] == s:
                    count = count + 1
                    if count == 2:
                        is_repeated = True
                        return is_repeated
                        break

            return is_repeated

    def isPalindrome(str):
        if str == '':
            return True
        else:
            for i in range(0, int(len(str) / 2)):
                if str[i] != str[len(str) - i - 1]:
                    return False
            return True




str = SuperStr("welcome to the world of python programming")
print(str.is_repeatance('s'))
print(str.isPalindrome('saas'))

the result is https://drive.google.com/file/d/1dHXK8RJs7vlRovULrUPS5uxRkX_oDNRO/view?usp=sharing the second method does not work but the first does even though it works this way if it is alone https://drive.google.com/file/d/1SBFnQ6OeDGtoTvo98SHeSdIbks8BTXnT/view?usp=sharing



Solution 1:[1]

What Avinash said in the comments should fix your problem. You need to include self in the method definition. e.g.

def isPalindrome(self, s):
    if s == '':
        return True
    else:
        for i in range(0, int(len(s) / 2)):
            if s[i] != s[len(s) - i - 1]:
                return False
        return True

The error you have is...

TypeError: isPalindrome() takes 1 positional argument but 2 were given

The error is saying that isPalindrome() has been defined with 1 positional argument. In your case this is the str in the def isPalindrome(str):. When you call it using the instance object str.isPalindrome('saas'), python adds the object itself as the first argument, so the method is called with 2 arguments. The object str and the value 'saas'.

Notice also that your def is_repeatance(self, s): method has self as an argument and works fine.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 pcoates