Gelly
Gelly

Reputation: 39

How to find/calculate the shaded area of this shape in python?

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

Answers (1)

Liam
Liam

Reputation: 563

Problem

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:

Solution 1 (Using different variable names)

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()

Solution 2 (Updating the 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()

Solution 3 (Moving calculations to the end)

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

Related Questions