so I've just begun learning python for a school course. I have to write a simple program that finds the first derivative of the equation
Y= Ax^4 + Bx^3 + Cx^2 + Dx + E
so far i can have the user input the variables, but one of the requirements is to have the program show the orignial formula with the inputed variables. because x is not defined, i keep coming up with an error message. The problem is that i need to have x undefined to show the user the original equation.
for example,
A = 2
B = 2
C = 2
D = 2
E = 2
i need the program to output
Y= 2x^4 + 2x^3 + 2x^2 + 2x + 2
How can I display this without defining x or having an error message?
11 3657
Here's a neat trick (slightly advanced, though). 
Formula = "Y= Ax^4 + Bx^3 + Cx^2 + Dx + E"


A = 2

B = 2

C = 2

D = 2

E = 2


vars = ('A', 'B', 'C', 'D', 'E')


result = Formula # Copy isn't really needed, but is a good habit


for var in vars:

result = result.replace(var, str(eval(var))) # eval() is the advanced trick #



print Formula

print result
Y= Ax^4 + Bx^3 + Cx^2 + Dx + E
Y= 2x^4 + 2x^3 + 2x^2 + 2x + 2
Here's a neat trick (slightly advanced, though). 
Formula = "Y= Ax^4 + Bx^3 + Cx^2 + Dx + E"


A = 2

B = 2

C = 2

D = 2

E = 2


vars = ('A', 'B', 'C', 'D', 'E')


result = Formula # Copy isn't really needed, but is a good habit


for var in vars:

result = result.replace(var, str(eval(var))) # eval() is the advanced trick #



print Formula

print result
Y= Ax^4 + Bx^3 + Cx^2 + Dx + E
Y= 2x^4 + 2x^3 + 2x^2 + 2x + 2
Wow thanks, that really worked well. I understand most of this code, but i dont understand what this means. 
for var in vars:

result = result.replace(var, str(eval(var)))
Wow thanks, that really worked well. I understand most of this code, but i dont understand what this means. 
for var in vars:

result = result.replace(var, str(eval(var)))
Hi shanazzle. I've added [code] tags to your post. Instructions for their use are on the right hand side of the page while you are posting.
Let me try to explain:
In Python, many objects are known as "iterable", meaning that you may get one element after another. To do that we use the in operator. The for operator signifies a loop. The vars variable that I defined is a tuple (an object that implements iteration very efficiently). So, for each item (or element) of vars, do the following:
Call the replace() method of the string object (result) with two arguments.
The first argument is simply the "A", etc pulled from the vars tuple by the in operator.
Next use the builtin function, eval() to ask the Python interpreter for the value of the variable A. The result of that is wrapped in the builtin str() function which converts objects to their string representation.
You may think if object oriented programming in very human language term this way:
The for/ in combination says: "Hey, mister tuple, each time I ask, please give me your next available item until there are no more to give (the var variable)".
Inside the loop, rely on a string's knowledge of itself: "Mister string, replace the text matched by the first argument with the contents of the second argument".
I hope that I have not just totally confused you.
Welcome to the Python Forum on TSDN,
Barton
Alright, thanks that makes sense, but then how do i turn the variables back into floating numbers so that i can multiply them, for example, when the derivative formula is
Y= 4*Ax^3 + 3*Bx^2 + 2*Cx + D
A = 2
B = 2
C = 2
D = 2
the result would be
4*2x^3 instead of the desired 8x^3 ??
Alright, thanks that makes sense, but then how do i turn the variables back into floating numbers so that i can multiply them, for example, when the derivative formula is
Y= 4*Ax^3 + 3*Bx^2 + 2*Cx + D
A = 2
B = 2
C = 2
D = 2
the result would be
4*2x^3 instead of the desired 8x^3 ??
That, my friend is a whole other ball of wax.
The task here is known as "parsing". You must be able to pick out (or parse) the various parts of the equation, finding which are numbers and which are operators and apply them accordingly.
This is a very advanced topic, so I must ask: Are you teaching your self?
I ask because I can't imagine an instructor throwing you into such a deep topic without much preparatory work.
That, my friend is a whole other ball of wax.
The task here is known as "parsing". You must be able to pick out (or parse) the various parts of the equation, finding which are numbers and which are operators and apply them accordingly.
This is a very advanced topic, so I must ask: Are you teaching your self?
I ask because I can't imagine an instructor throwing you into such a deep topic without much preparatory work.
OK, I take most of that back. Using another trick (the builtin exec() function) it is possible to have the interpreter parse the formula for you:  Formula = "Y= Ax^4 + Bx^3 + Cx^2 + Dx + E"


A = 2

B = 2

C = 2

D = 2

E = 2


vars = ('A', 'B', 'C', 'D', 'E')


result = Formula # Copy isn't really needed, but is a good habit

x = 2


for var in vars:

result = result.replace("%sx" %var, "%s*%s" %(str(eval(var)), str(x)))


print Formula

print result

exec(result)

print Y
I've added a bunch of "string formating" to the original. Or, more simply: 
x = 2


for var in vars:

result = result.replace("x", "*%s" %str(x))
You'll need to be aware of operator precedence and group with parentheses accordingly.
Both forms return the same end result. Here's the latter versions output:
Y= Ax^4 + Bx^3 + Cx^2 + Dx + E
Y= A*2^4 + B*2^3 + C*2^2 + D*2 + E
3
OK, I take most of that back. Using another trick (the builtin) exec() function it is possible to have the interpreter parse the formula for you:  Formula = "Y= Ax^4 + Bx^3 + Cx^2 + Dx + E"


A = 2

B = 2

C = 2

D = 2

E = 2


vars = ('A', 'B', 'C', 'D', 'E')


result = Formula # Copy isn't really needed, but is a good habit

x = 2


for var in vars:

result = result.replace("%sx" %var, "%s*%s" %(str(eval(var)), str(x)))


print Formula

print result

exec(result)

print Y
I've added a bunch of "string formating" to the original. Or, more simply: 
x = 2


for var in vars:

result = result.replace("x", "*%s" %str(x))
You'll need to be aware of operator precedence and group with parentheses accordingly.
Both forms return the same end result. Here's the latter versions output:
Y= Ax^4 + Bx^3 + Cx^2 + Dx + E
Y= A*2^4 + B*2^3 + C*2^2 + D*2 + E
3
Here's the corrected version and output: 
for var in vars:

result = result.replace("x", "*%s" %str(x))

result = result.replace("^", "**")
Y= ((Ax)^4) + ((Bx)^3) + ((Cx)^2) + (Dx) + E
Y= ((A*2)**4) + ((B*2)**3) + ((C*2)**2) + (D*2) + E
342
Here's the corrected version and output: 
for var in vars:

result = result.replace("x", "*%s" %str(x))

result = result.replace("^", "**")
Y= ((Ax)^4) + ((Bx)^3) + ((Cx)^2) + (Dx) + E
Y= ((A*2)**4) + ((B*2)**3) + ((C*2)**2) + (D*2) + E
342
Here is the simplest yet: 
Formula = "Y= ((Ax)^4) + ((Bx)^3) + ((Cx)^2) + (Dx) + E"


A = 2

B = 2

C = 2

D = 2

E = 2


x = 2


result = Formula # Copy isn't really needed, but is a good habit


result = result.replace("x", "*x")

result = result.replace("^", "**")



print Formula

print result

exec(result)

print Y



Y= ((Ax)^4) + ((Bx)^3) + ((Cx)^2) + (Dx) + E

Y= ((A*x)**4) + ((B*x)**3) + ((C*x)**2) + (D*x) + E

342

Here is the simplest yet: 
Formula = "Y= ((Ax)^4) + ((Bx)^3) + ((Cx)^2) + (Dx) + E"


A = 2

B = 2

C = 2

D = 2

E = 2


x = 2


result = Formula # Copy isn't really needed, but is a good habit


result = result.replace("x", "*x")

result = result.replace("^", "**")



print Formula

print result

exec(result)

print Y



Y= ((Ax)^4) + ((Bx)^3) + ((Cx)^2) + (Dx) + E

Y= ((A*x)**4) + ((B*x)**3) + ((C*x)**2) + (D*x) + E

342

That makes sense, but see thats just defining x as a number. what I need to do is multiply the inputted variable by a number, ex 4*Ax^3, and say you input A = 4, what it needs to do is recognize 4*4x^3 equals 16x^3
That makes sense, but see thats just defining x as a number. what I need to do is multiply the inputted variable by a number, ex 4*Ax^3, and say you input A = 4, what it needs to do is recognize 4*4x^3 equals 16x^3
Go ahead and post some of your work here. You should have enough to have made an honest attempt at it by now.
Thanks.
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
4 posts
views
Thread by Sven Dzepina 
last post: by

1 post
views
Thread by Russell Blau 
last post: by

20 posts
views
Thread by Brian Kazian 
last post: by

9 posts
views
Thread by Stud Muffin 
last post: by
               