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