1

When I call the mainfile, the below mention validate functions do not execute as expected. What changes shall I make with the mainfile so that the validate methods execute themselves?

x = input('enter a number')

def mainfile(data):
  def validate(self,data):
    if int(data) > 3:
     print( 'greater than 3')
  def validate1(self, data):
    if int(data) > 2:
      print( 'greater than 2')

mainfile(x);
1
  • 1
    You even didn't call them. Commented Apr 16, 2020 at 7:56

3 Answers 3

3

Firstly those methods don't belong to a class, so the self keyword is not needed here and must be removed to avoid a Type error due to missing positional arguments.

Secondly, you don't call the nested functions validate and validate1, so hence they are never executed. You must directly call them from the mainfile function. E.g. validate(data) validate1(data)

Sign up to request clarification or add additional context in comments.

Comments

1

You are only defining validate, you arne't calling it:

def mainfile(data):
  def validate(self,data):
    if int(data) > 3:
     print( 'greater than 3')
  def validate1(self, data):
    if int(data) > 2:
      print( 'greater than 2')

  # You could, e.g., call them here:
  validate(data)
  validate1(data)

Comments

1

You have defined the functions validate and validate1, but they are not called if you only use the function mainfile. Also, you only need the keyword 'self' in the context of classes, so you can just leave it out here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.