![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Nov 2005
Location: Spring Valley, CA
Posts: 52
Rep Power: 3
![]() |
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: ") |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Dec 2005
Posts: 118
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#4 | |
|
Programmer
Join Date: May 2005
Posts: 48
Rep Power: 0
![]() |
Quote:
|
|
|
|
|
|
|
#5 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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! 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) |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Nov 2005
Location: Spring Valley, CA
Posts: 52
Rep Power: 3
![]() |
Hey thx. Ill c what I can do...
|
|
|
|
|
|
#7 |
|
Programmer
Join Date: Nov 2005
Location: Spring Valley, CA
Posts: 52
Rep Power: 3
![]() |
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] |
|
|
|
|
|
#8 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Usually you put the imports at the top of the program, but yeah, 'tis great.
|
|
|
|
|
|
#9 |
|
Programmer
Join Date: Nov 2005
Location: Spring Valley, CA
Posts: 52
Rep Power: 3
![]() |
Bitchin. thanks.
![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|