[英]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.