In: Computer Science
Imagine that you are a user in a hurry that types 2O (oh) rather than 20 (zero) as the height. What happens to your program? If the user enters a value that could not be converted to numeric type, allow 3 additional opportunities to enter a new value. If the user fails to enter a correct value after 4 attempts, inform them of such failure and allow the program to end without crashing.
Question: Can someone help me with this?
My original code:
# I put the area calculations of both the triangle and trapezoid
up, so that the code could run through them both properly.
def triangle(height, base):
area_triangle = height * base * 0.5
print "the area of your triangle equals %s" % (area_triangle)
def trapezoid(height, base1, base2):
area_trapezoid = ((base1 + base2) / 2) * height
print "the area of your trapezoid equals %s" %
(area_trapezoid)
# Giving the option to choose triangle or trapezoid area.
shapes = ["triangle", "trapezoid"]
# I gave my code a title and put my name on it.
print "Area calculator"
print "Made by Alison Voigt"
print "-------------------------------"
# Asking the user to enter either the triangle or the trapezoid to
move on to the area calculation.
def calculation():
print "please type the name of your shape"
print "(triangle, trapezoid)"
# Once the user chooses a shape they are asked to enter in the
height, and base(s) for the shape of their choosing.
user_shape = raw_input()
if user_shape == shapes[1]:
trapezoid(height = float(raw_input("please type the height of the
trapezoid.")), base1 = float(raw_input("please type the base1
length of the trapezoid.")), base2 = float(raw_input("please type
the base2 length of the trapezoid.")))
elif user_shape == shapes[0]:
triangle(height = float(raw_input("please type the height of the
triangle.")), base = float(raw_input("please type the base length
of the triangle.")))
# If the user enters in wrong answer the code will tell you to try
again.
else:
print "That's not in the choices!, Try again."
calculation()
# The user is now given the option to calculate the same shape or
the other.
calculation()
choice = raw_input("Would you like to calculate the area of a
different shape?(yes/no)")
while choice == "yes":
print "---------------------"
calculation()
Please find below the updated python program. A new unction getShapeParam has been introduced to separately handle shape parameter related user inputs and their conversion to floats.
OUTPUT