简体   繁体   English

方法 b. is_palindrom() 不起作用

[英]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?你能帮我看看为什么第二种方法在 class 中不起作用,即使它单独工作?

the question is问题是

Develop a SuperStr class that inherits the functionality of the standard str type and contains 2 new methods: a.开发一个SuperStr class,它继承了标准str类型的功能,包含2个新方法: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. is_repeatance(s) 方法,接受1个参数s,根据字符串s重复integer次是否能得到当前行,返回True或False。 Return False if s is not a string.如果 s 不是字符串,则返回 False。 Assume that the empty string contains no repetitions.假设空字符串不包含重复。 b. b. is_palindrom() method, which returns True or False depending on whether the string is a palindrome. is_palindrom() 方法,它根据字符串是否为回文返回 True 或 False。 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结果是https://drive.google.com/file/d/1dHXK8RJs7vlRovULrUPS5uxRkX_oDNRO/view?usp=sharing第二种方法不起作用,但第一种方法有效,即使它是单独的https://drive。 google.com/file/d/1SBFnQ6OeDGtoTvo98SHeSdIbks8BTXnT/view?usp=sharing

What Avinash said in the comments should fix your problem. Avinash 在评论中所说的应该可以解决您的问题。 You need to include self in the method definition.您需要在方法定义中包含self eg例如

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.错误是说isPalindrome()已用 1 个位置参数定义。 In your case this is the str in the def isPalindrome(str): .在您的情况下,这是def isPalindrome(str): str的 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' .当您使用实例 object str.isPalindrome('saas')调用它时,python 会将 object 本身添加为第一个参数,因此使用 2 arguments 调用该方法。object str和值'saas'

Notice also that your def is_repeatance(self, s): method has self as an argument and works fine.另请注意,您的def is_repeatance(self, s):方法将self作为参数并且工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 In Django views.py I create a session in method A and try to read it in class B. Error: attribute session not defined in class B - In Django views.py I create a session in method A and try to read it in class B. Error: attribute session not defined in class B celeryd -B。 如何在后台运行它? - manage.py celeryd -B. How to run it in background? 该函数接受两个矩阵作为输入,并返回一个A * B的矩阵。在Python中 - A function that takes two matrices as input, and returns a matrix with A * B. In Python 当 b 是列表时,为什么 b+=(4,) 有效而 b = b + (4,) 无效? - Why does b+=(4,) work and b = b + (4,) doesn't work when b is a list? any([a == b for a, b in zip(string, string[1:])]) 是如何工作的 - how does any([a == b for a, b in zip(string, string[1:])]) work pow(a, b, c) 如何解决这个问题? - How does pow(a, b, c) work for this problem? Python `import AB` 不起作用,但 `from A import B` 起作用 - Python `import A.B` does not work but `from A import B` works 检查以短语 = A,B 给出的输入对中的字谜,其中 A 和 B 是 2 个单词 - check for anagram in pair of input given as phrase = A, B. where A and B are 2 words 元组 (a,b)=(b,a) 中成员的交换如何在内部工作? - How does swapping of members in tuples (a,b)=(b,a) work internally? 为什么`a - Why does `a<b<c` work in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM