Animeartist
Animeartist

Reputation: 1187

Trouble writing python program?

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?

Upvotes: 1

Views: 98

Answers (4)

MrE
MrE

Reputation: 20778

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.

Upvotes: 1

Kai Xiao
Kai Xiao

Reputation: 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()

Upvotes: -1

jbihan
jbihan

Reputation: 3099

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...

Upvotes: 1

Shahriar
Shahriar

Reputation: 13804

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

Upvotes: 3

Related Questions