Python: making a reference to a function not yet defined

ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.

Having a problem logging in? Please visit this page to clear all LQ-related cookies.

Introduction to Linux - A Hands on Guide

This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.

Click Here to receive this Complete Guide absolutely free.

Python: making a reference to a function not yet defined

I'm learning Python, and I've made a couple of simple mathematical programs and I'm wanting to combine the two scripts. Here's the two seperate programs:

Code:

def multiply():
def intro():
print "Yo bitches, I can multiply. Wanna watch?"
intro()
def inputs():
n1=input("What's the first number?: ")
n2=input("Now gimme the second number: ")
p=n1*n2
print "I just did it, motherfucker. The answer is", p, "units."
inputs()
def again():
again=raw_input("Are you going to mathematically rape me again?(y or n): ")
if again=='y':
print inputs()
if again=='Y':
print inputs()
if again=='n':
print "Fuck, and I was looking forward to that."
if again=='N':
print "Fuck, and I was looking forward to that."
again()
multiply()

Code:

def circle():
def intro():
print "Welcome to my circumference finder!"
intro()
def main():
r=input("What is the radius of the circle? ")
c=r*r*3.14
print "The circumference is", c, "units"
again=raw_input("Do you want to do another calculation? (y or n) ")
if again=='y':
print main()
elif again=='Y':
print main()
else:
print "Good bye"
main()
circle()

So I tried combining the two like this.

Code:

def choose():
print "1. Multiply numbers"
print "2. Find the circumference of a circle"
choice=raw_input("Choose which program you would like to use: ")
if choice=='1':
print multiply()
elif choice=='2':
print circle()
else:
print "Please choose a valid option"
print choose()
choose()
def multiply():
def intro():
print "Yo bitches, I can multiply. Wanna watch?"
intro()
def inputs():
n1=input("What's the first number?: ")
n2=input("Now gimme the second number: ")
p=n1*n2
print "I just did it, motherfucker. The answer is", p, "units."
inputs()
def again():
again=raw_input("Are you going to mathematically rape me again?(y or n): ")
if again=='y':
print inputs()
if again=='Y':
print inputs()
if again=='n':
print "Fuck, and I was looking forward to that."
if again=='N':
print "Fuck, and I was looking forward to that."
again()
multiply()
def circle():
def intro():
print "Welcome to my circumference finder!"
intro()
def main():
r=input("What is the radius of the circle? ")
c=r*r*3.14
print "The circumference is", c, "units"
again=raw_input("Do you want to do another calculation? (y or n) ")
if again=='y':
print main()
elif again=='Y':
print main()
else:
print "Good bye"
main()
circle()

When I run the programs seperately, they run fine. When I run the last one and choose the program to run, it says that multiply or circle is not defined. How do I define it without running it before choose()?

define all your functions first, then run them. in this case, you just want to run choose, so it should look like this.

Code:

def choose():
print "1. Multiply numbers"
print "2. Find the circumference of a circle"
choice=raw_input("Choose which program you would like to use: ")
if choice=='1':
print multiply()
elif choice=='2':
print circle()
else:
print "Please choose a valid option"
print choose()
def multiply():
def intro():
print "Yo bitches, I can multiply. Wanna watch?"
intro()
def inputs():
n1=input("What's the first number?: ")
n2=input("Now gimme the second number: ")
p=n1*n2
print "I just did it, motherfucker. The answer is", p, "units."
inputs()
def again():
again=raw_input("Are you going to mathematically rape me again?(y or n): ")
if again=='y':
print inputs()
if again=='Y':
print inputs()
if again=='n':
print "Fuck, and I was looking forward to that."
if again=='N':
print "Fuck, and I was looking forward to that."
again()
def circle():
def intro():
print "Welcome to my circumference finder!"
intro()
def main():
r=input("What is the radius of the circle? ")
c=r*r*3.14
print "The circumference is", c, "units"
again=raw_input("Do you want to do another calculation? (y or n) ")
if again=='y':
print main()
elif again=='Y':
print main()
else:
print "Good bye"
main()
choose

if for some reason that still fails, put your def choose() block after the other two function definitions.

LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.