Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
It is giving an error saying isbalanced is undefined

What I have tried:

class Check:
    def isbalanced(self,s):
        a=len(s)
        count=0
        for i in range(0,a):
            if s[i]=='(':
                count=count+1
            else:
                count=count-1
        if count>0:
            return False
        else:
            return True
    def callin(self,s1,s2):
        if (isbalanced(s1+s2)):
            return True
        else:
            return isbalanced(s2+s1)
c=input("Enter string\n")
d=input("Enter string\n")
if (callin(c,d)):
    print("Balanced")
else:
    print("Not Balanced")
Posted
Updated 28-Jul-18 6:34am

1 solution

Try this:
Python
class Check:
    def isbalanced(self,s):
        a=len(s)
        count=0
        for i in range(0,a):
            if s[i]=='(':
                count=count+1
            else:
                count=count-1
        if count>0:
            return False
        else:
            return True
    def callin(self,s1,s2):
        if (self.isbalanced(s1+s2)):    # note correct use of self. prefix
            return True
        else:
            return self.isbalanced(s2+s1) # ditto

c=input("Enter string\n")
d=input("Enter string\n")
foo = Check()    # create an instance of the Check class

if (foo.callin(c,d)):    # call the callin method on the class instance
    print("Balanced")
else:
    print("Not Balanced")
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900