Question

In: Computer Science

Imagine that you are a user in a hurry that types 2O (oh) rather than 20...

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()

Solutions

Expert Solution

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


Related Solutions

Imagine that rather than the government moving to close the GDP gap, the central bank decided...
Imagine that rather than the government moving to close the GDP gap, the central bank decided to move instead. Specify what type of open market operations would it undertake to close the GDP Gap (inflationary gap)
Imagine that the Earth’s rotational axis was tilted 90 degrees rather than 23.5 degrees (so the...
Imagine that the Earth’s rotational axis was tilted 90 degrees rather than 23.5 degrees (so the polar axis would point towards the Sun twice a year). Describe the apparent motion of the Sun and stars over a 24-h period on the winter solstice from (a) the North Pole, (b) the equator, and (c) the South Pole. Note that the Sun’s angular diameter is 0.5 degrees.
In what types of ecosystems would you find chemoautotrophs rather than photoautotrophs? What trophic level does...
In what types of ecosystems would you find chemoautotrophs rather than photoautotrophs? What trophic level does each group occupy? In the Lake Ontario ecosystem, how would you characterize the trophic positions of the opossum shrimp and sea lamprey? Distinguish the source of energy utilized by chemoautotrophs versus photoautotrophs.
Why would a manufacturing firm use departmental rates rather than one factory overhead (OH) rate? Describe...
Why would a manufacturing firm use departmental rates rather than one factory overhead (OH) rate? Describe an advantage and a disadvantage of using departmental rates.
Why might it be better to use these types of designs rather than the independent-samples design?
Why might it be better to use these types of designs rather than the independent-samples design?
Imagine that you suffer from amnesia. Would you rather it be anterograde amnesia or retrograde amnesia?...
Imagine that you suffer from amnesia. Would you rather it be anterograde amnesia or retrograde amnesia? Why? How would this type of amnesia affect your life?
please describe the types of jobs that exist in the organization that you imagine you will...
please describe the types of jobs that exist in the organization that you imagine you will be working in as a healthcare administrator in the future. Provide a broad overview of the types of workers that exist in your organization in the following categories: Medical Professionals (physicians, nurses) Allied Health Professionals Healthcare Administrators You do not have to write out a comprehensive explanation of all jobs in the organization, but please address the types of jobs that represent the primary...
The literature value for the Ksp of Ca(OH)2 at 25 °C is 4.68E−6. Imagine you ran...
The literature value for the Ksp of Ca(OH)2 at 25 °C is 4.68E−6. Imagine you ran the experiment and got a calculated value for Ksp which was too high. Select all of the possible circumstances which would cause this result. A. The HCl was more concentrated than the labeled molarity (0.0500 M). B. The Ca[OH]2 solution may have been supersaturated. C. The HCl was less concentrated than the labeled molarity (0.0500 M). D. The Ca[OH]2 solution may have been unsaturated....
Imagine that you are planning a user-based usability test to evaluate a new interface that allows...
Imagine that you are planning a user-based usability test to evaluate a new interface that allows people to track online their medical information, such as blood tests, diagnostics, annual check-ups, and patient visits. Since many governments have set the goal to move to full electronic patient records in the next few years, this is an important project. Doctors will also use this application but, for this exercise, we’re focused on patients. Where might you want to recruit potential participants? Would...
Let’s imagine that you sent a ship towards the Sun, and at a distance of 20...
Let’s imagine that you sent a ship towards the Sun, and at a distance of 20 million km (2x10^10 m), which is halfway between the Sun and Mercury, you suddenly opened the reflective solar sail and thrusted towards Pluto. Estimate the velocity of the spacecraft as it reaches back the Earth on its way towards Pluto. Assume that the initial velocity was 0 (when sail was deployed close to the Sun). The distance from the Sun to the Earth is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT