1

There's a main function, inside which the entire operation is done. Inside it, there's an if statement. I want to define a function inside that if function only if that if is satisfied. The compiler should proceed to the function defined in it and 3/4 of the program depends on the operations inside the if. When I run the program, the statements above if only is getting executed.

eg:

  def manage():
         name=raw_input("Enter name")
         pass=raw_input("Enter password")
         conf=raw_input("Confirm password")
         if(pass==conf):
               print"You can proceed!!!"
               def proceed():
                       #here rate is calculated
                       print rate
   manage()#at the very end

the statements above the if is getting executed only.the function inside if is not getting executed.i gave the statements in the else,but its not coming. is it possible to define function inside an if? if not,is there any package we should import? or is there some other way?

5
  • you dont execute the function inside the if , you just defined it ! if you want that you need to call it , with proceed() Commented Jan 2, 2015 at 18:21
  • You create the function, but never call it. But it would be better to define it elsewhere, and only call it in the if block. Commented Jan 2, 2015 at 18:21
  • 4
    pass is a keyword, you shouldn't use that as a variable name Commented Jan 2, 2015 at 18:21
  • Well your inner function proceed will be created if if(pass==conf) comes true. But just like you called manage function the same way you need to call proceed funtion too. Commented Jan 2, 2015 at 18:22
  • Thank you all!! your answers helped me!! Commented Jan 2, 2015 at 19:04

4 Answers 4

3

You can't use pass as variable name:

def manage():
    name=raw_input("Enter name: ")
    Pass=raw_input("Enter password: ")
    conf=raw_input("Confirm password: ")
    if(Pass==conf):
        print"You can proceed!!!"
        proceed()

def proceed():
    print rate

manage()

You can't use Python Keywords as variable name

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

2 Comments

This also never calls proceed.
I thought OP will get it. Thanks anyway
1

You only declare the method inside the if, you don't execute it. Here is what you should do:

def manage():
    name=raw_input("Enter name")
    passwd=raw_input("Enter password")
    conf=raw_input("Confirm password")
    if(passwd==conf):  
        print"You can proceed!!!"
        proceed()

def proceed():
    #here rate is calculated
    print rate

manage() #at the very end

You should:

  • declare your method proceed outside the manage method
  • Call the proceed method when you enter the if
  • change the pass variable name as it is a Python keyword

And this also assumes that the variable rate is declared and contains something...

Comments

1

I think you might want to review what a function is and does.

A function is defined within a scope, and can be within another function, but that doesn't mean it has to be inside, otherwise there is no real point to a function.

So, in short, you need to define the function(s) in the body, and call the function of interest when needed.

Directly from your code:

def proceed():
         #here rate is calculated
         print rate

def manage():
         name=raw_input("Enter name")
         passwd=raw_input("Enter password")
         conf=raw_input("Confirm password")
         if(passwd==conf):
               print"You can proceed!!!"
               proceed()

manage()#at the very end

I would suggest you pick up a good book on Python, like this one, and read through it.

As noted below, the pass variable name is a reserved keyword in Python and will break your code. Renamed here as passwd.

1 Comment

true, i missed the pass keyword that is illegal. Corrected now.
-1

It seems you defined the function in the if statement, but didn't call the function. Try add proceed() after the definition. Something like this:

if(pass==conf):
               print"You can proceed!!!"
               def proceed():
                       #here rate is calculated
                       print rate
               proceed()

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.