48.8k views
15 votes
Problem: Write the Python code of a program called ComputeSquare that asks the user to enter the length of the side of the square and displays the area and perimeter of the square. Check that the side length is greater than zero, and if it is not, display an informative error message to the user. ​

User Asaf Pala
by
5.0k points

1 Answer

6 votes

Solution :

The given problem is solved in Python.

def ComputeSquare():

side = float(input('Enter the side of the square: '))

if side > 0:

perimeter = 4 * side

area = side * side

print('Perimeter of the square is:',

perimeter, 'unit.')

print('Area of the square is:', area, 'square unit.')

else:

print('Invalid input.')

ComputeSquare()

Explanation :-

  • In this program, we create a function ComputeSquare() to calculate the perimeter and area of a square.

  • The function asks the user to enter the side of the square. The side is then stored in a variable.

  • Now, we check whether the side is greater than 0 or not using if-else statement.

  • If the condition is true, the perimeter and area is calculated which is displayed using print() statement.

  • If the condition is false, the else blocks executes printing the error message.

Refer to the attachment for output.

Problem: Write the Python code of a program called ComputeSquare that asks the user-example-1
User Jtepe
by
4.1k points