► Python program to calculate area of a square


Steps followed:
1. take length of side of square as input from user
2. validate user input by converting to float
3. calculate area of a square with formula side * side
4. print area of a square


Program

#python program to calculate area of a square

#take side of square as input from user
side=input("Enter length of side of square: ")

try:
    side=float(side)

    #calculate area of a square
    area = side * side

    #print area of a square
    print("---output---")
    print("area of a square with side %0.2f is %0.2f"%(side,area))

except:
    print("---error---")
    print("please enter a valid length")


 


Output

Enter length of side of square: 4
---output---
area of a square with side 4.00 is 16.00

 

Enter length of side of square: length
---error---
please enter a valid length





Also Read: