Home>

I'm new to the program.
I am writing a program in python that outputs a message that matches a constant.
I want to output a result like dict,
It doesn't work because name'messages' is not defined.
Please tell me how to deal with it.

class Error Class:
    messages = {
        'E400001':'The number of digits is strange',
        'E400002':'The type is wrong',
        'E400003':'The value does not exist. '
    }
    def set_errorMassage (code):
        if code in messages:
            self.error_info = {'code': code,'message': messages ['code'],'httpcode': code [1: 4]}
        else: else:
            error_info = {'code':'E400999','message':'This operation cannot be performed. ',' httpcode': '500'}
        return self.error_info
    def get_errorMassage ():
        if self.error_info is not null:
            error_info = {'code':'','message':'','httpcode': '200'}
        return self.error_info
if __name__ == "__main__":
    set_errorMassage ('E400001')
    print (get_errorMassage)
// set_errorMassage ('E400001')
{'code':'E400001','message':'The number of digits is wrong',' httpcode': '400'}
// By the way, if set_errorMassage is not set<when ​​there is no error>
{'code':'','message':'','httpcode': '200'}
// When you get an unclear error
{'code':'E400999','message':'This operation cannot be performed. ',' httpcode': '500'}
  • Answer # 1

    Since messages are contained within the ErrorClass class, they can only be used via ErrorClass.
    So even if it says messages, it's undefined.

    Isn't ErrorClass unnecessary?
    Looking only at this process, ErrorClass is unnecessary. You can use it by sending messages to the top rubell.

    If ErrorClass is required
    If you need this class, you need to access it as a class variable.
    Just replace the messages in your code with ErrorClass.messages and you're good to go.

  • Answer # 2

    functionset_errorMassageToselfWith argumentsmessagesToself.messagesI think you should edit it to.
    Also, the function name isset_error_messageIs better or correct.MassageIt's a massage.