Reputation: 39
I'm trying to write a python program to calculate the shaded area of the circle in this picture:
[![enter image description here][1]][1]
Here is the input and output:
Enter the radius of a circle: 10
Enter the side of square :3
Enter the width of a rectangle :4
Enter the length of a rectangle :2
The shaded area is : 12.0
Here is my code:
PI = 3.14
radius = float(input("Enter the radius of a circle:"))
area = PI * radius ** 2
side = int(input("Enter the side of square:"))
area = side*side
width = float(input("Enter the width of a rectangle:"))
length = float(input("Enter the length of a rectangle:"))
area = width * length
perimeter = (width + length) * 2
print("The shaded area is :", perimeter)
Upvotes: 2
Views: 653
Reputation: 563
You are overwriting the value of area
and the previous assigned values are lost.
Example
area = input('enter first value')
10
# the value of area is 10
area = input('enter second value')
3
# the value of area is now 3 and 10 is lost
However you can use area -= number
or area += number
to substract or add a number to the current value of the variable like so:
area = input('enter first value')
10
# the value of area is 10
area -= input('enter second value')
3
# the value of area is now 10 - 3 = 7
As correctly pointed out in the comments you should also import math
and use math.pi
instead of using the value 3.14
The correct output for the shaded area is:
Enter the radius of a circle:10
Enter the side of square:3
Enter the width of a rectangle:4
Enter the length of a rectangle:2
The shaded area is : 297.1592653589793
>
I would recommend solution 1 as variables should have meaningful names (unlike solution 2):
and solution 3 is a bit harder to read and understand:
import math
def findDimensions():
radius = float(input("Enter the radius of a circle:"))
# radius = 10
circle_area = math.pi * radius ** 2
side = int(input("Enter the side of square:"))
# side = 3
square_area = side * side
# but you could use square_area = side ** 2 as well
width = float(input("Enter the width of a rectangle:"))
# width = 4
length = float(input("Enter the length of a rectangle:"))
# length = 2
rectangle_area = width * length
shaded_area = circle_area - square_area - rectangle_area
print("The shaded area is :", shaded_area)
findDimensions()
area
variable instead of overwriting it)import math
def findDimensions():
radius = float(input("Enter the radius of a circle:"))
# radius = 10
area = math.pi * radius ** 2
side = int(input("Enter the side of square:"))
# side = 3
area -= side * side
# but you could use area -= side ** 2 as well
width = float(input("Enter the width of a rectangle:"))
# width = 4
length = float(input("Enter the length of a rectangle:"))
# length = 2
area -= width * length
print("The shaded area is :", area)
findDimensions()
import math
def findDimensions():
radius = float(input("Enter the radius of a circle:"))
# radius = 10
side = int(input("Enter the side of square:"))
# side = 3
width = float(input("Enter the width of a rectangle:"))
# width = 4
length = float(input("Enter the length of a rectangle:"))
# length = 2
shaded_area = (math.pi * radius ** 2) - (side * side) - (width * length)
print("The shaded area is :", shaded_area)
findDimensions()
Upvotes: 2