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

Expert's answer

2017-05-22T12:06:10-0400

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

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS