def square(l):
area=l*l
return area
def rectangle(l, w):
area=l*w
return area
print("Please enter a number to calculate the area of shape")
print("Square = 1")
print("Rectangle = 2")
user_input = 0
while user_input not in (1,2):
user_input = int(input("Enter your choice: "))
if (user_input==1):
print("Let's calculate the area of Square")
length = int(input("Length: ")
area = square(length)
print("Area of Square is: ", area)
if (user_input==2)
print("Let's calculate the area of Rectangle")
length = int(input("Length: ")
width = int(input("Width: ")
area = rectangle(length, width)
print("Area of Rectangle is: ", area)
Calculating an area of shape (problem)
Page 1 of 16 Replies - 527 Views - Last Post: 09 October 2012 - 07:56 AM
#1
Calculating an area of shape (problem)
Posted 07 October 2012 - 10:07 AM
Hello, I am developing a very simple program which helps me to calculate the area of 2D shapes but I am having a slight problem when running the program. In line 20, it is giving me a syntax error and highlighting the error at 'area'. In addition how to add a loop to my program and I am using Python 3.0.
Replies To: Calculating an area of shape (problem)
#2
Re: Calculating an area of shape (problem)
Posted 07 October 2012 - 03:17 PM
You are missing a closing ')' on line 19 (and 25 and 26 as well)
Sorry just saw second component of your question. What kind of loop are you after? There are quite a few good Python tutorials to be found on the 'net, just Google 'Python 3 loops' (or similar)
Sorry just saw second component of your question. What kind of loop are you after? There are quite a few good Python tutorials to be found on the 'net, just Google 'Python 3 loops' (or similar)
This post has been edited by scalt: 07 October 2012 - 03:18 PM
#3
Re: Calculating an area of shape (problem)
Posted 09 October 2012 - 07:12 AM
Thank you scalt, didn't knew this simple mistake could cause an error. Well I am not sure type of loops that would be good for my program. I extended my program and received errors
Line 81: triangle() missing 1 required positional argument: 'h'
Line 88: name 'b' is not defined
Line 95: ellipse() missing 1 required positional argument: 'b'
Line 109: trapezoid() missing 2 required positional arguments: 'b' and 'h'
Line 123: rectangle_prism() missing 2 required positional arguments: 'w' and 'h'
Line 130: pyramid() missing 1 required positional argument: 'h'
Line 137: cone() missing 1 required positional argument: 'h'
Line 143: name 'math' is not defined
Error
- Calculating the area of circular shapes (circle, ellipse, cone, sphere)
- Line 74: name 'math' is not defined
- Line 81: triangle() missing 1 required positional argument: 'h'
- Line 88: name 'b' is not defined
- Line 101: ellipse() missing 1 required positional argument: 'b'
- Line 103: trapezoid() missing 2 required positional arguments: 'b' and 'h'
- Line 117: rectangle_prism() missing 2 required positional arguments: 'w' and 'h'
- Line 124: pyramid() missing 1 required positional argument: 'h'
- Line 131: cone() missing 1 required positional argument: 'h'
Well here are some of the errors of my extended version of my program, for some reason when I am calculating the area & volume of shapes, the math.pi has a different value. For example, when I was calculating the area of circle, where r = 1; area = 3.1415926...
In addition is there a way that I can separate my menu into two sections, meaning that if I click a number, it goes to the next page.
Thank you for further advancement, I am still learning Python and still got a lot more to learn, but I am enjoying this programming language though.
Line 81: triangle() missing 1 required positional argument: 'h'
Line 88: name 'b' is not defined
Line 95: ellipse() missing 1 required positional argument: 'b'
Line 109: trapezoid() missing 2 required positional arguments: 'b' and 'h'
Line 123: rectangle_prism() missing 2 required positional arguments: 'w' and 'h'
Line 130: pyramid() missing 1 required positional argument: 'h'
Line 137: cone() missing 1 required positional argument: 'h'
Line 143: name 'math' is not defined
import math
def square(l):
area=l*l
return area
def rectangle(l, w):
area=l*w
return area
def circle(r):
area=math.pi*2*r
return area
def triangle(b, h):
area=0.5*b*h
return area
def parallelogram(b, h):
area=b*h
return area
def ellipse(a, B)/>:
area=math.pi*a*b
return area
def trapezoid(a, b, h):
area=(0.5*(a+B)/>*h)
return area
def cube(l):
area=l*l*l
return area
def rectangle_prism(l, w, h):
area=l*w*h
return area
def pyramid(b, h):
area= 0.33333333*b*h
return area
def cone(r, h):
area=0.33333333*math.pi*r*r*h
return area
def sphere(r):
area=1.33333333*math.pi*r*r*r
return area
print("Please enter a number to calculate the area of shape")
print("\t\t\t Menu\n==========================================================\n1. Square\t\t2. Rectangle\t\t\t3. Circle\n4. Triangle\t\t5. Parallelogram\t\t6. Ellipse\n7. Trapezoid\t8. Cube\t\t\t\t\t9. Rectangle Prism\n10. Pyramid\t\t11. Cone\t\t\t\t12. Sphere\n==========================================================")
user_input = 0
while user_input not in (1,2,3,4,5,6,7,8,9,10,11,12):
user_input = int(input("Enter your choice: "))
if (user_input==1):
print("Let's calculate the area of Square")
length = float(input("Length: "))
area = square(length)
print("Area of Square is: ", area)
if (user_input==2):
print("Let's calculate the area of Rectangle")
length = float(input("Length: "))
width = float(input("Width: "))
area = rectangle(length, width)
print("Area of Rectangle is: ", area)
if (user_input==3):
print("Let's calculate the area of Circle")
radius = float(input("Radius: "))
area = circle(radius*2*math.pi)
print("Area of Circle is: ", area)
if (user_input==4):
print("Let's calculate the are of Triangle")
base = float(input("Base: "))
height = float(input("Height: "))
area = triangle(0.5*base*height)
print("Area of Triangle is ", area)
if (user_input==5):
print("Let's calculate the area of Parallelogram")
base = float(input("Base: "))
height = float(input("Height: "))
area = parallelogram(b*h)
print("Area of Parallelogram is ", area)
if (user_input==6):
print("Let's calculate the area of Ellipse")
a = float(input("Side a: "))
b = float(input("Side b: "))
area = ellipse(math.pi*a*B)/>
print("Area of Ellipse is, ", area)
if (user_input==7):
print("Let's calculate the area of Trapezoid")
a = float(input("Side a: "))
b = float(input("Side b: "))
height = float(input("Height: "))
area = trapezoid(0.5*(a+B)/>*height)
print("Area of Trapezoid is, ", area)
if (user_input==8):
print("Let's calculate the volume of Cube")
l = float(input("Length: "))
area = cube(length*length*length)
print("Volume of Cube is, ", area)
if (user_input==9):
print("Let's calculate the volume of Rectangle Prism")
l = float(input("Length: "))
w = float(input("Width: "))
h = float(input("Height "))
area = rectangle_prism(length*width*height)
print("Volume of Rectangle Prism is, ", area)
if (user_input==10):
print("Let's calculate the volume of Pyramid")
b = float(input("Base: "))
h = float(input("Height: "))
area = pyramid(1.33333333*base*height)
print("Volume of Pyramid is, ", area)
if (user_input==11):
print("Let's calculate the volume of Cone")
r = float(input("Radius: "))
h = float(input("Height: "))
area = cone(0.33333333*math.pi*radius*radius*height)
print = ("Volume of Cone is, ", area)
if (user_input==12):
print("Let's calculate the volume of Sphere")
r = float(input("Radius: "))
area = sphere(1.33333333*math.pi*radius*radius*radius)
print = ("Volume of Sphere is, ", area)
Error
- Calculating the area of circular shapes (circle, ellipse, cone, sphere)
- Line 74: name 'math' is not defined
- Line 81: triangle() missing 1 required positional argument: 'h'
- Line 88: name 'b' is not defined
- Line 101: ellipse() missing 1 required positional argument: 'b'
- Line 103: trapezoid() missing 2 required positional arguments: 'b' and 'h'
- Line 117: rectangle_prism() missing 2 required positional arguments: 'w' and 'h'
- Line 124: pyramid() missing 1 required positional argument: 'h'
- Line 131: cone() missing 1 required positional argument: 'h'
Well here are some of the errors of my extended version of my program, for some reason when I am calculating the area & volume of shapes, the math.pi has a different value. For example, when I was calculating the area of circle, where r = 1; area = 3.1415926...
In addition is there a way that I can separate my menu into two sections, meaning that if I click a number, it goes to the next page.
Thank you for further advancement, I am still learning Python and still got a lot more to learn, but I am enjoying this programming language though.
#4
Re: Calculating an area of shape (problem)
Posted 09 October 2012 - 07:21 AM
When you define a function like:
def f(one, two)...
you need to supply all the required arguemnts.
def f(one, two)...
you need to supply all the required arguemnts.
#5
Re: Calculating an area of shape (problem)
Posted 09 October 2012 - 07:26 AM
What does it means? Can you show an example?
#6
Re: Calculating an area of shape (problem)
Posted 09 October 2012 - 07:29 AM
Spoiler
#7
Re: Calculating an area of shape (problem)
Posted 09 October 2012 - 07:56 AM
You must, must be consistent.
If you don't define it, the program won't see it. Indeed, rather than all those two liner calculation functions, which can be one liners, you'd be better off making all those if user_entry sections into their own functions.
Always look to shorten your code. If you see yourself doing the same thing over and over, try to figure out how to stick it in a function and do less work.
base = float(input("Base: "))
height = float(input("Height: "))
# you have loaded variables base and height
area = parallelogram(b*h) # what the hell is b and h?
# perhaps
area = parallelogram(base*height) # what the hell is b and h?
If you don't define it, the program won't see it. Indeed, rather than all those two liner calculation functions, which can be one liners, you'd be better off making all those if user_entry sections into their own functions.
Always look to shorten your code. If you see yourself doing the same thing over and over, try to figure out how to stick it in a function and do less work.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|