Write to ask the user input the width and length of a rectangle and then the area and the perimeter of this rectangle.

Write to ask the user input the width and length of a rectangle and then the area and the perimeter of this rectangle.

LANGUAGE:  PYTHON

CHALLENGE:

a)Write to ask the user input the width and length of a rectangle and then the area and the perimeter of this rectangle.

b) do the input validation to make sure that the user will not input a negative number.

SOLUTION:


width, height = -1, -1 
# Input width + validate input
whilewidth < 0:    
    try :     
        width = int(raw_input("width="))    
    except : # not a number    
        width = -1 
whileheight < 0:    
    try :     
         height = int(raw_input("height="))    
    except :    
        height = -1 
area = width * height
perimeter = 2 * width + 2 * height
print("Area     : "+ str(area))
print("Perimeter: "+ str(perimeter))
raw_input("Press anykey to exit...")

Add comment