16.2k views
14 votes
The following function is supposed to return -1 when x is negative, +1 when x is positive, or if x is zero. What, if anything, is wrong with the function? def plusMinusZero(x): if x == 0 : return 0 elif x <= 0 : return -1 else x >= 0 : return 1 A. A return statement must be added at the end of the function B. Both occurrences of elif must be replaced with if C. The <= and >= must be replaced with < and > D. Nothing is wrong with the function

User Sapht
by
5.5k points

1 Answer

1 vote

def dx(fn, x, delta=0.001):

return (fn(x+delta) - fn(x))/delta

def solve(fn, value, x=0.5, maxtries=1000, maxerr=0.00001):

for tries in xrange(maxtries):

err = fn(x) - value

if abs(err) < maxerr:

return x

slope = dx(fn, x)

x -= err/slope

raise ValueError('no solution found')

User LachoTomov
by
5.2k points