Reputation: 113
I am working om a simple Python exercise: ask the user for a mathematical formula f(x), which is then evaluated to find the minimum/maximum value of x between x=0 and x=10, with a sample rate of x/100. The code I wrote is as follows:
from math import *
def compute_min_and_max():
Fx = input("what is the desired function of x?" )
listY = []
for i in range(0,1001):
x = i/100
y = eval(Fx)
listY.append(y)
print("This function's minimum value is", min(listY))
print("This function's maximum value is", max(listY))
It works fine, except when the user asks for a formula like log(x), which returns a domain error (for x=0), or 1/(x-a) (which returns a division by 0 error when a=x). How do I, in these cases, return a text string to the user that informs them that there is no min/max value in the interval (or, when as is the case for log(x), there is a maximum value but not a minimum value, how do I print the max value?)
Upvotes: 1
Views: 672
Reputation: 30258
Wrap your statements with an try: except: else: block
Fx = input("what is the desired function of x?" )
listY = []
try:
for i in range(0,1001):
x = i/100
y = eval(Fx)
listY.append(y)
except ZeroDivisionError:
print("Divide by zero error for f({})".format(x))
except ValueError:
print("Invalid value for f({})".format(x))
else:
print("This function's minimum value is", min(listY))
print("This function's maximum value is", max(listY))
I'm not sure how you can define there was a min or a max when you have undefined results. You could use limits around the undefined result(s) to understand which direction you approached the undefined value.
But if you want to simply return the min or max of the values that were defined you can move the try block inside the for loop, e.g:
Fx = input("what is the desired function of x?" )
listY = []
undefined = False
for i in range(0,1001):
try:
x = i/100
y = eval(Fx)
except ZeroDivisionError:
print("Divide by zero error for f({})".format(x))
undefined = True
except ValueError:
print("Invalid value for f({})".format(x))
undefined = True
else:
listY.append(y)
if undefined:
print("Ignoring undefined results")
print("This function's minimum value is", min(listY))
print("This function's maximum value is", max(listY))
Upvotes: 3
Reputation: 1
Just add an if condition before the for loop and check whether x = 0 or not. if x = 0 then just print a text string otherwise run your for loop and print the result.
from math import *
def compute_min_and_max():
Fx = input("what is the desired function of x?" )
listY = []
for i in range(0,1001):
x = i/100
y = eval(Fx)
listY.append(y)
if x = 0:
print('divided by zero')
else:
print("This function's minimum value is", min(listY))
print("This function's maximum value is", max(listY))
Upvotes: -1
Reputation: 50076
This is were python's Exception handling should be used. An example:
try:
foo = 1/0
# catch a specific error
except ZeroDivisionError:
foo = 0
print("Division by zero")
# catch all
except Exception as err:
foo = None
print("Something really bad happened:", err)
In your case, you should wrap the start/end values in a try: ... except: ...
to catch whether the function is defined at the extremes.
Note that checking whether there is a max/min for arbitrary functions is probably not possible by sampling! Any +/- inf values may be at arbitrary points which you do not sample at all.
Upvotes: 0