Coderflo
Coderflo

Reputation: 1

Checking multiple for items in a for loop python

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()(]")

Upvotes: 0

Views: 437

Answers (2)

Christopher Shroba
Christopher Shroba

Reputation: 7544

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()(]")

Upvotes: 0

Jean-François Fabre
Jean-François Fabre

Reputation: 140168

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:

Upvotes: 2

Related Questions