Answer to Question #189038 in Python for Process

Question #189038

What is the definition of precondition and post condition statement


1
Expert's answer
2021-05-04T11:31:28-0400
# A precondition is a condition that must be met at the beginning of a function
#or program in order for it to work correctly.

# Postconditions are those conditions that guarantee that a function or program
# will return the correct result.

# For example, take the function of calculating the real roots of the equation
def get_square_root(a, b, c):
    assert all([type(i) is int or  type(i) is float for i in (a,b,c)]),"Input Error"
    assert b**2-4*a*c>=0,"Calculation Error"
    return (-b+(b**2-4*a*c)**.5)/(2*a), (-b-(b**2-4*a*c)**.5)/(2*a)
# In this case
# assert all([type(i) is int or  type(i) is float for i in (a,b,c)]),"Input Error"
# these are preconditions the correct operation of the function is possible 
# only with numeric arguments 
# assert b**2-4*a*c>=0,"Calculation Error" 
# this postcondition guarantees the derivation of only 
# real roots of the equation

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!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS