Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 9th, 2005, 10:18 PM   #1
snipertomcat
Programmer
 
snipertomcat's Avatar
 
Join Date: Nov 2005
Location: Spring Valley, CA
Posts: 52
Rep Power: 3 snipertomcat is on a distinguished road
New to Functions

Hi. This is my first program with the use of functions. Could I get some opinions? Could it be improved at all?

#By Snipertomcat
#Area Calculator w/ menu interface

def menu_options(): #Getting all the def's squared away
    print "Welcome to Area-Calc."
    print "Options:"
    print "  'p' print options"
    print "  'r' calculate area of a rectangle "
    print "  's' calculate area of a square "
    print "  'c' calculate area of a circle "
    print "  'q' quit the program"

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

def area_square(num):
        return num*num
    
def area_circle(radius):
    return 3.14 * radius **2

menu_options() #Program starts here

choice = "p"
while choice != "q":
    if choice == "r":
        w = input("Rectangle width: ")
        h = input("Rectangle hight: ")
        print "The area is: ",area_rec(w,h)
    elif choice == "s":
        squarenum = input("Square length: ")
        print "The area is: ", area_square(squarenum)
    elif choice == "c":
        rad = input("Radius: ")
        print "The area is: ", area_circle(rad)
    choice = raw_input("Option: ")
snipertomcat is offline   Reply With Quote
Old Dec 10th, 2005, 6:48 AM   #2
Klipt
Hobbyist Programmer
 
Join Date: Dec 2005
Posts: 118
Rep Power: 0 Klipt is an unknown quantity at this point
If you import math and use math.pi instead of 3.14 you'll get a more accurate result.

I think it's considered good practice to put the code for your main program into a function called main() and call that. Makes it easier to say things like:

if __name__ == '__main__':
    main()

If you don't want that part to run when you import the file just to use its calculation functions, for example.
Klipt is offline   Reply With Quote
Old Dec 10th, 2005, 10:26 AM   #3
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Don't get confused here, your main() has nothing to do with __main__

What happens to choice 'p'?
__________________
I looked it up on the Intergnats!

Last edited by Dietrich; Dec 10th, 2005 at 10:36 AM.
Dietrich is offline   Reply With Quote
Old Dec 10th, 2005, 2:16 PM   #4
deathseeker25
Programmer
 
Join Date: May 2005
Posts: 48
Rep Power: 0 deathseeker25 is on a distinguished road
Quote:
Originally Posted by Dietrich
Don't get confused here, your main() has nothing to do with __main__

What happens to choice 'p'?
That's right....i also didn't understand what happens if choice ="p"...
deathseeker25 is offline   Reply With Quote
Old Dec 10th, 2005, 5:06 PM   #5
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Nothing - "p" is a dummy character so the program goes straight to the raw_input.

Sniper, I think it's awesome. A couple of things: decide whether you're going to use spacing around your operators or not (I recommend you do) this:
2+2=4     # evil!
2 + 2 = 4 # woo!
Also, as Klipt said, use math.pi:
import math

def area_circle(radius):
    return math.pi * radius ** 2

# better - this doesn't import the whole damn math library:
from math import pi

def area_circle(radius):
    return pi * radius ** 2
    # you don't need the "math." as you're specifying the thingy you want to use (pi)
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Dec 10th, 2005, 9:33 PM   #6
snipertomcat
Programmer
 
snipertomcat's Avatar
 
Join Date: Nov 2005
Location: Spring Valley, CA
Posts: 52
Rep Power: 3 snipertomcat is on a distinguished road
Hey thx. Ill c what I can do...
snipertomcat is offline   Reply With Quote
Old Dec 10th, 2005, 9:49 PM   #7
snipertomcat
Programmer
 
snipertomcat's Avatar
 
Join Date: Nov 2005
Location: Spring Valley, CA
Posts: 52
Rep Power: 3 snipertomcat is on a distinguished road
this one better?

[HTML]
#By Snipertomcat
#Area Calculator w/ menu interface

print "Welcome to Area-Calc."

def menu_options(): #Getting all the def's squared away
print "Options:"
print " 'p' print options"
print " 'r' calculate area of a rectangle "
print " 's' calculate area of a square "
print " 'c' calculate area of a circle "
print " 'q' quit the program"

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

def area_square(num):
return num * num

import math

def area_circle(radius):
return math.pi * radius ** 2

menu_options() #Program starts here


def main():
choice = "p"
while choice != "q":
if choice == "r":
w = input("Rectangle width: ")
h = input("Rectangle hight: ")
print "The area is: ",area_rec(w,h)
elif choice == "s":
squarenum = input("Square length: ")
print "The area is: ", area_square(squarenum)
elif choice == "c":
rad = input("Radius: ")
print "The area is: ", area_circle(rad)
elif choice == "p":
menu_options()
choice = raw_input("Option: ")

main()
[/HTML]
snipertomcat is offline   Reply With Quote
Old Dec 11th, 2005, 4:18 PM   #8
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Usually you put the imports at the top of the program, but yeah, 'tis great.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Dec 11th, 2005, 7:43 PM   #9
snipertomcat
Programmer
 
snipertomcat's Avatar
 
Join Date: Nov 2005
Location: Spring Valley, CA
Posts: 52
Rep Power: 3 snipertomcat is on a distinguished road
Bitchin. thanks.
snipertomcat is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:32 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC