Question #68273

4. 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.

Expert's answer

Answer on Question #68273, Programming & Computer Science / Python

Question:

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
while width < 0:
    try:
        width = int(raw_input("width="))
    except: # not a number
        width = -1
while height < 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 any key to exit...")


Answer provided by www.AssignmentExpert.com

LATEST TUTORIALS
APPROVED BY CLIENTS