Click here to Skip to main content
14,971,553 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This is for my project. My problem is I don't know how to display an error message inside my class function code. Error message must display if the user entered a non-parenthesis character.

What I have tried:

open_list=['(','[','{']
close_list=[')',']','}']

def check(mystr):
    stack=[]
    for i in mystr:
        if i in open_list:
            stack.append(i)
        elif i in close_list:
            pos=close_list.index(i)
            if ((len(stack)>0)) and (open_list[pos]==stack[len(stack)-1]):
                stack.pop()
            else:
                return"Invalid"
            if len(stack)==0:
                return"Valid"
            else:
                return"Invalid"
                
while True:
        s=input("Enter your parentheses:")
        string=[{},[],()]
        print(s,'-',check(s))
        if s in string:
            print("Well done!")
        else:
            print("Error! please enter a valid parentheses")


I just need to put an error message if the user entered a non-parenthesis character.
Thank you in advance.

I need help in part of the error message. Error message must show if the user entered non-parentheses input.
Posted
Updated 13-Jan-21 20:05pm
v5
Comments
Richard MacCutchan 12-Jan-21 6:34am
   
you need to iterate all the input characters and test to see that they are one of the valid set in list a. If you find a character that is not in the set then print the error message and reject the input string.
bnhr 12-Jan-21 8:06am
   
I also had that in my mind but I don't know how to start or how to do it.. Do I need to make another def func? or ???..Also thanks
Richard MacCutchan 12-Jan-21 8:23am
   
Do it in function f. Something like:
for c in str:
    if c not in a:
        # a bad character

I am not sure why you have duplicated the valid characters in a, you should only need them once.
bnhr 12-Jan-21 9:45am
   
Thank you for the ideas and for sharing what's wrong with it... I will try it. Thanks!
Richard MacCutchan 14-Jan-21 4:21am
   
You have updated your post, but it is not clear if it is now solved, or you have a further problem.
bnhr 14-Jan-21 8:57am
   
it was still on the error message but now it was solved.. Thank you

1 solution

Would suggest you to use User-Defined Exception in Python using try-except

Refer: How to Define Custom Exceptions in Python? (With Examples)[^]

Example from there:
Python
# define Python user-defined exceptions
class Error(Exception):
    """Base class for other exceptions"""
    pass


class ValueTooSmallError(Error):
    """Raised when the input value is too small"""
    pass


class ValueTooLargeError(Error):
    """Raised when the input value is too large"""
    pass


# you need to guess this number
number = 10

# user guesses a number until he/she gets it right
while True:
    try:
        i_num = int(input("Enter a number: "))
        if i_num < number:
            raise ValueTooSmallError
        elif i_num > number:
            raise ValueTooLargeError
        break
    except ValueTooSmallError:
        print("This value is too small, try again!")
        print()
    except ValueTooLargeError:
        print("This value is too large, try again!")
        print()

print("Congratulations! You guessed it correctly.")
   
Comments
bnhr 12-Jan-21 6:23am
   
I tried that one.. Thanks

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