第五季 发表于 2020-8-18 17:28:14

可以帮我看看这段代码错在哪儿吗

class Nstr:
    def __init__(self,str1='',str2=''):
      self.str1=str1
      self.str2=str2
    def edg(self):
      for each in self.str2:
            for i in range(len(self.str1)):
                if self.str1==each:
                  count=self.str1.pop(i)
      return count
a=Nstr('diwuji','wi')
print(a.edg())

Twilight6 发表于 2020-8-18 18:09:05




字符串不能使用 pop() ,你的代码想达到什么目的?

是统计子字符串吗?还是什么?

看看这个代码是不是你想要的?

class Nstr:
    def __init__(self,str1='',str2=''):
      self.str1=str1
      self.str2=str2
    def edg(self):
      for each in self.str2:
            for i in self.str1:
                if each == i:
                  self.str1 = self.str1.replace(each,'')
      return self.str1
a=Nstr('diwuji','wi')
print(a.edg())


陈尚涵 发表于 2020-8-18 18:34:22

Twilight6 发表于 2020-8-18 18:09
字符串不能使用 pop() ,你的代码想达到什么目的?

是统计子字符串吗?还是什么?


我也不知道他想干什么,楼主,回复一下您要干什么

第五季 发表于 2020-8-18 19:26:37

秒懂 谢谢哥哥
页: [1]
查看完整版本: 可以帮我看看这段代码错在哪儿吗