# I have another beginner question.

[b][red]This message was edited by djshadmego at 2002-12-8 17:22:40[/red][/b][hr]

I am also trying to learn Python for some back end database storage for future recall into a calendar. I'm trying out Python and Perl actually. I'm stuck in a program that has to have a separate function for the area of a square, the area of a rectangle, and the area of a circle. The program should include a menu interface. I have written it for the most part, but I am trying to cycle it as many times as the user wants to keep going and having a problem with the program following my map. If anyone can help that would be great. Thanks.
[code]def hello():
print 'Hello!'

def areasq(width):
return width**2

def arearec(width,height):
return width*height

pi = 3.14

def print_welcome(name):
print 'Welcome,',name

hello(),
print_welcome(name)
print
print "a rectangle, or a circle."
print
def mainq(question):
return question
question = raw_input("Which one would you like to know? ")

if question == 'square':
w = input("Width: ")
while w <= 0:
print "This number must be a positive number. (Anything above '0')"
w = input("Width: ")
print "The area of a Square is the width times itself or w**2"
print "Width = ",w," so the area =",areasq(w)
print

quest = raw_input("Would you like to do another one? ")
if quest == 'yes':
mainq(question)
else:
print "Have a nice day then!"

if question == 'rectangle':
w = input("Width: ")
while w <= 0:
print "This number must be a positive number. (Anything above '0')"
w = input("Width: ")
h = input("Height: ")
while h <= 0:
print "This must be positive also."
h = input("height: ")
print "The area of a rectangle is the width times the height or w*h"
print "Width =",w,"Height =",h," so the area =",arearec(w,h)
print
q = raw_input("Would you like to do another one? ")
if q == 'yes':
mainq(question)
else:
print "Have a nice day then!"

if question == 'circle':
while r <= 0:
print "Alas, this must be a positive number. Please try again."
print "Pi =",pi,"Radius = ",r," so the area of the circle =",areacir(r)
q = raw_input("Would you like to do another one? ")
if q == 'yes':
mainq(question)
else:
print "Have a nice day then!"
[/code]

• : [b][red]This message was edited by djshadmego at 2002-12-8 17:22:40[/red][/b][hr]
:
: I am also trying to learn Python for some back end database storage for future recall into a calendar. I'm trying out Python and Perl actually. I'm stuck in a program that has to have a separate function for the area of a square, the area of a rectangle, and the area of a circle. The program should include a menu interface. I have written it for the most part, but I am trying to cycle it as many times as the user wants to keep going and having a problem with the program following my map. If anyone can help that would be great. Thanks.

I think the main problem is that you want a loop, but you haven't written your code in any form that is conducive to looping. Here's my first shot at cleaning up your code and putting it in a form that demonstrates the idea of a user-terminated endless loop:

[code]
pi = 3.14

def hello():
print 'Hello!'

def areasq(width):
return width**2

def arearec(width,height):
return width*height

def prompt_for_name():

def print_welcome(name):
print 'Welcome,',name

print
print "a rectangle, or a circle."
print "Which one would you like to know? "

if __name__ == '__main__':
hello()

continue_looping = True
while(continue_looping):

#process selection
if selection == 'square':
w = input("Width: ")
while w <= 0:
print "This number must be a positive number. (Anything above '0')"
w = input("Width: ")
print "The area of a Square is the width times itself or w**2"
print "Width = ",w," so the area =",areasq(w)
print
elif selection == 'rectangle':
w = input("Width: ")
while w <= 0:
print "This number must be a positive number. (Anything above '0')"
w = input("Width: ")
h = input("Height: ")
while h <= 0:
print "This must be positive also."
h = input("height: ")
print "The area of a rectangle is the width times the height or w*h"
print "Width =",w,"Height =",h," so the area =",arearec(w,h)
print
elif selection == 'circle':
while r <= 0:
print "Alas, this must be a positive number. Please try again."
print "The area is pi,",pi,"times the radius squared or 3.14*r**2"
print "Pi =",pi,"Radius = ",r," so the area of the circle =",areacir(r)

repeat = raw_input("Would you like to do another one? ")
if repeat.upper() not in ['Y', 'YES']:
continue_looping = False

else:
print "Have a nice day,", username, "!"
[/code]
• [code]
: def mainq(question):
: return question
: question = raw_input("Which one would you like to know? ")

*snip*

: q = raw_input("Would you like to do another one? ")
: if q == 'yes':
: mainq(question)
: else:
: print "Have a nice day then!"
: [/code]

When you call a function, you are not telling Python to jump to where that function is defined and go from there. You are telling Python to execute the code in that function and return a value (if any) to you [italic]where you are when you call the function[/italic]. Functions define blocks of code that you can call any number of times within the regular flow of your program. They cannot (or at least, should not) control the flow of your program. For flow control, you use conditional expressions (if) and loops (while, for).
• I understand what I was trying to do and what I did wrong. However, I would like to know what this is. Namely, the "_" before and after (name) and (main). Are they needed? What purpose do they serve? Thank you for the help and I will show you what I did lately and how I am improving!

: [code]
: if __name__ == '__main__':
: hello()
: [/code]

• : I understand what I was trying to do and what I did wrong. However, I would like to know what this is. Namely, the "_" before and after (name) and (main). Are they needed? What purpose do they serve? Thank you for the help and I will show you what I did lately and how I am improving!

Every object in python has some built-in properties and methods. They are denoted with two underscores before and after their name. To see what I'm talking about, start up your python interpreter and type the command:

dir()

You should see something like:

['__builtins__', '__doc__', '__name__']

This is a list of the defined objects in your namespace. Try:

dir(__builtins__)

You should see a list of objects that are defined in the __builtins__ module. Now type:

import sys
sys.__name__

You should see:

'sys'

Every object in python has a __name__ property automatically which holds the name of the object. Now, at the python prompt, type:

__name__

You should see:

'__main__'

The module that is executed by the interpreter gets the special name, '__main__'. If you're running the interpreter then the interpreter itself has the name, '__main__'. If you call a python script from the command line like:

python shapes.py

Then the shapes.py module will have the string '__main__' in it's __name__ property. If, on the other hand, you did an "import shapes" command in the interpreter or from another script, your module would have 'shapes' as it's __name__ and the module that did the import would be '__main__'.

So if you look at the code I wrote, if you run the script by itself, __name__ will be equal to '__main__', so the code in the body of the if statement will execute. If I just wanted to use the functions defined in the script somewhere else, I could import the exact same file and that code would not execute because __name__ would not equal '__main__'.

[size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

• : The module that is executed by the interpreter gets the special name, '__main__'. If you're running the interpreter then the interpreter itself has the name, '__main__'. If you call a python script from the command line like:

python shapes.py

Then the shapes.py module will have the string '__main__' in it's __name__ property. If, on the other hand, you did an "import shapes" command in the interpreter or from another script, your module would have 'shapes' as it's __name__ and the module that did the import would be '__main__'.

So if you look at the code I wrote, if you run the script by itself, __name__ will be equal to '__main__', so the code in the body of the if statement will execute. If I just wanted to use the functions defined in the script somewhere else, I could import the exact same file and that code would not execute because __name__ would not equal '__main__'.

I think I understand. It is basically a way to tell the computer where the script is coming from and how and where to execute it. It would come in handy when trying to imcorporate two or more languages together to get something done?
• : I think I understand. It is basically a way to tell the computer where the script is coming from and how and where to execute it.

Not exactly. Let's say you write a set of functions in a module and you write a quick script to test them out. If you import that module into another script, you don't want that test code to execute, so you tuck it in under the if __name__ == '__main__' block. That way if you run the module itself, the code executes. If you import the module into another module, that code does not execute. That's it.

: It would come in handy when trying to imcorporate two or more languages together to get something done?

Not really, no. It only really works from within python. Any other language calling your python script would have to call the python interpreter, which would make your script '__main__'. It's really mainly used for "self-test" code, but it is a good idea to use it so you can import your modules into other modules and not worry about other programs running.
[size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]