Python Forum

Full Version: Derivative operation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
print('Now you are creating your function, when finished type Break')

def f(x):
    total=0
    a=0
    b=0
    while a!='Break' or b!='Break':
        a=input('Enter the coefficient of X:')
        b=input('Enter the power of X:')
        if a=='Break' or b=='Break':
            return total 
            break
        else:
            c=int(a)*x**int(b)
            total = total+c
        
e=0.00001
x=int(input('Enter the X value:'))
r= f((x+e)-f(x))/e

print(r, 'is the value of derivative', 'f(x)','for the X value of',x)
Purpose of this code is it creates a function from the inputs by the user and finds the value of its derivative for a X value again input by the user.
The problem is that when I type Break to a or b input while loop doesn't break.
I would appreciate any suggestion or help.
Thank you in advance.
The break doesn't happen because return total stops the loop.
It does work its just that it might not seem to because the function f is called twice.
you 'Enter the X value:' it goes into the first f loop you type Break to one of the inputs,
it then goes into the 2nd f loop , you have to type Break to one of the inputs again to exit.
(May-06-2019, 09:33 PM)Yoriz Wrote: [ -> ]The break doesn't happen because return total stops the loop. It does work its just that it might not seem to because the function f is called twice. you 'Enter the X value:' it goes into the first f loop you type Break to one of the inputs, it then goes into the 2nd f loop , you have to type Break to one of the inputs again to exit.
So I have to define the same function twice for f(x) and for f(x+e) right? Is there any way to define both functions together