Find a string

  • + 1 comment

    Need Help!
    Can anyone help me understand what is wrong with my code? I keep trying to understand but i cant get it right.

    def count_substring(string, sub_string):
        cout = 0
        flag = False
        l2 = list(sub_string)
        n2 = len(sub_string)
        l1 = list(string)
        n1 = len(string)
        for i in range(n1-n2+1):
            if l1[i]==l2[0]:
                for x in range(n2):
                    if l1[i+x] == l2[x]:
                        flag = True
                    else:
                        flag = False
                if flag:
                    cout+=1
                    flag = False
        
        return cout
    
    if __name__ == '__main__':
        string = input().strip()
        sub_string = input().strip()
        
        count = count_substring(string, sub_string)
        print(count)
    
    • + 2 comments

      I think the issue is that you are overcomplicating it (too many lines), so you'll likely make lots of hard to fix small errors.

      I recommend restarting from scratch with online help.

      • + 0 comments

        I've already done it. Yep had to start from the scratch. BTW thanks for the response. 😊