LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-07-2006, 05:24 PM   #1
JRR883
Member
 
Registered: Aug 2005
Location: God's barf bag
Distribution: Ubuntu 7.04 (Feisty Fawn)
Posts: 86

Rep: Reputation: 15
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()?
 
Old 02-08-2006, 11:28 AM   #2
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
don't run the function after you define it

def: multiply
...
multiply()

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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Python: find defined text string in a file, and replace the whole line Dark Carnival Programming 6 05-22-2007 06:02 AM
user defined function in kernel vishalbutte Programming 4 02-07-2006 06:01 AM
Dlopen function - UNDEFINED REFERENCE mamthababu Programming 1 07-19-2005 10:20 AM
Undefined reference to function error Quest101 Linux - Newbie 0 12-30-2004 05:01 PM
function, pass by reference question true_atlantis Programming 5 04-02-2004 11:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration