► Python program to calculate area of a rectangle


Steps followed:
1. take length and breadth of a rectangle as input from user
2. validate user input by converting to float
3. calculate area of rectangle using formula l*b
4. print area of rectangle


Program

#python program to calculate area of a rectangle

#take length and breadth of a rectangle as input from user
length=input("Enter length of rectangle: ")
breadth=input("Enter length of rectangle: ")

try:
    length=float(length)
    breadth=float(breadth)

    #calculate area of rectangle
    area = length * breadth

    #print area of rectangle
    print("---output---")
    print("area of rectangle with length %0.2f and breadth %0.2f is %0.2f"%(length,breadth,area))

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


Output

Enter length of rectangle: 5
Enter length of rectangle: 20
---output---
area of rectangle with length 5.00 and breadth 20.00 is 100.00

 

Enter length of rectangle: s
Enter length of rectangle: fi
---error---
please enter a valid length





Also Read: