[Tutor] Can I use def without ( ) at the end?

Brian van den Broek bvande at po-box.mcgill.ca
Sun Jul 10 02:54:21 CEST 2005


Nathan Pinno said unto the world upon 09/07/2005 20:36:

<top post corrected>

>  ----- Original Message -----  From: "Brian van den Broek" 
> 
>  > Nathan Pinno said unto the world upon 09/07/2005 19:03:
>  >> Hi all,
>  >>
>  >> Is the subject possible without getting an error?
>  >>
>  >> Nathan Pinno
>  >> Crew, McDonalds Restaurant, Camrose, AB Canada
>  >> http://www.npinnowebsite.ca/
>  >
>  > No. Why do you want to do this? If it is to have a function with no
>  > arguments:
>  >
>  > >>> def no_args():
>  > ... print "I've no arguments"
>  > ...
>  > >>> no_args()
>  > I've no arguments
>  > >>>
>  >
>  > Brian vdB
> 
> 
> 
>   Also, I just wanted to know because I'm using it in my Giant Computer 
>  program for the menus, so I don't have to keep re-typing them.
>  
>   Nathan Pinno


Nathan,

I'm afraid I don't know what you meant in your other post by "get a 
def". I'm also unclear on what it is you feel that you are having to 
retype.

The usual way to have some functions and 'drive' them through a text 
menu is either a dictionary dispatch (don't worry about that right 
now) or something simpler like:


 >>> def function_1():
... 	print "I am function 1!"
... 	
 >>> def function_2():
... 	print "I am function 2!"
... 	
 >>> def menu():
...     print "I'm a simple menu system."
... 	print "Enter 1 for function 1"
... 	print "Enter 2 for function 2"
... 	choice = raw_input("Your selection please?\n")
... 	if choice == '1':
... 		function_1()
... 	elif choice == '2':
... 		function_2()
... 	else:
... 		print "Please read more carefully"

Try that at the prompt, and then call the menu function -- on a line, 
type:

menu()

If that isn't helping, you are going to have to explain more clearly 
what problem it is that you are trying to avoid.

Brian vdB



More information about the Tutor mailing list