Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to make a program that will ask user to enter a string of parenthesis, validate the order of parenthesis (must be close to '(,),{,},[,],(),{},[]' as valid and '(},{{{,{][[)' as invalid) and give an error message if the user entered a non-parenthesis character.

I haven't done the error message code but I got this
TypeError: unbound method f() must be called with validity instance as first argument (got type instance instead)

What I have tried:

class validity:
    def f(str):
        a= ['()', '{}', '[]'] 
        while any(i in str for i in a):
            for j in a:
                str = str.replace(j, '') 
        return not str 
while True:
     s = input("Enter parentheses : ")
     print(s, "-", "is Valid" 
           if validity.f(s) else "is Invalid")



What should I do to correct it?
Posted
Updated 11-Jan-21 23:01pm
v2

1 solution

You need to create an instance of the validity class, and use that instance to call the function. Something like:
Python
v = validity() # create a validity instance
while True:
     s = input("Enter parentheses : ")
     print(s, "-", "is Valid" 
           if v.f(s) else "is Invalid") # use the instance v to call the function f

See 9. Classes — Python 3.7.9 documentation[^].
 
Share this answer
 
Comments
lil_mint 12-Jan-21 6:22am    
thank you..I forgot about that one

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