0

I've written a code to tell the user that the their brackets are not balanced.

I can exactly tell where my code is going wrong.

Once it comes across the first situation of brackets, it does not continue to look for the wrong ones or other right ones (I think).

I want to keep it simple but long (ie no fancy shortcuts for now)

Here's my code

def isbalanced(text):
    openingbracket=[]
    for i, next in enumerate (text):
        if next=='(' or next=='{' or next=='[':
            openingbracket.append(next)
        if next==')' or next=='}' or next==']':
            if len(openingbracket)==0:
                print("ops only opening brackets")
                return False
            else:
                a=openingbracket.pop()
                if a =='(' and next==')':
                    print("its matched parenthesis")
                    return True
                if a =='{' and next=='}':
                    print("its matched curly brackets")
                    return True
                if a =='[' and next==']':
                    print("its matched square")
                    return True
                else:
                    print("wrong closing brackets")
                    return False
    if len(openingbracket):
        print ("no closing brackets")
        return False
    else:
        print("no brackets")
        return True
isbalanced("Hello()(]")
0

2 Answers 2

2

if you return as soon as something is OK, you won't find the errors further in the string... that's exactly what you're doing in 3 places:

if a =='(' and next==')':
    print("its matched parenthesis")
    return True  # let it continue to check the string.

Add a return True at the end of your method: if the loop goes through, then string is OK (actually it's already OK in your current code)

Aside:

  • don't use next for your variable, as it's built-in to get a value from an iterable. I'll use c
  • if next=='(' or next=='{' or next=='[': could be replaced by if c in "({[":
  • for i, next in enumerate (text): you're not using the index, just do for c in text:
Sign up to request clarification or add additional context in comments.

1 Comment

I made all the suggested changes by everyone. Now how about if I want to check for the index of the unmatched open bracket after my loop runs. Ie the in ()([] i want to print the index of the third bracket '[' because it's unmatched, how might I achieve this?
0

Simply remove your return True statements, since those cause the entire method to return True, before checking the rest of the string. The only time you know you can return True is once you've processed the entire string, so the only return True should be after your for loop finishes.

def isbalanced(text):
    openingbracket=[]
    for i, next in enumerate (text):
        if next=='(' or next=='{' or next=='[':
            openingbracket.append(next)
        if next==')' or next=='}' or next==']':
            if len(openingbracket)==0:
                print("ops only opening brackets")
                return False
            else:
                a=openingbracket.pop()
                if a =='(' and next==')':
                    print("its matched parenthesis")
                    return True                     # REMOVE THIS LINE
                if a =='{' and next=='}':
                    print("its matched curly brackets")
                    return True                     # REMOVE THIS LINE
                if a =='[' and next==']':
                    print("its matched square")
                    return True                     # REMOVE THIS LINE
                else:
                    print("wrong closing brackets")
                    return False
    if len(openingbracket):
        print ("no closing brackets")
        return False
    else:
        print("no brackets")
        return True
isbalanced("Hello()(]")

1 Comment

Thanks Christopher. In the back of my head, I knew it must have been a silly mistake somewhere. It worked and now I just have to twig it a bit more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.