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 3813
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Sven Dzepina 
last post by:
Hello people =)
Has somebody a nice script, which can solve equations ?
It would be super, if someone has an idea where I can get such a script /
code in php.
Thanks.
Gretting!

by: Russell Blau 
last post by:
This is not really a Python question, but it does relate to a Python program
I'm working on, so this seems like as good a place as any to ask for
suggestions ...
I'm working on a GUI application...

by: Brian Kazian 
last post by:
Here's my problem, and hopefully someone can help me figure out if there is
a good way to do this.
I am writing a program that allows the user to enter an equation in a text
field using...

by: Stud Muffin 
last post by:
Hey
Basically, I'm trying to take objects created in microsoft word using
equation editor (for creating clean looking math/physics equations)
and putting them into some sort of webpage format....

by: w33nie 
last post by:
My table is pretty well complete, but I would prefer it if the value for Points could be turned into a mathematical equation,
and this equation would use the data from the other fields in the table...

by: Trev17 
last post by:
Hello, I am new to C++ and i have tried for several hours to make a program my teacher has given me as a lab. Here is the Lab question:
the roots of the quadratic equation ax^2 + bx + c = 0, a...

by: jlynx23 
last post by:
gud am...i am currently working with a project of mine which is about derivative...and i am having some problem with the printing of the derivative...
here is the block of code i used:
/* print...

by: madman228 
last post by:
Hi guys I have run in to a littl bit of trouble. I am writing a class called polynomial
in which i need a derivative method I have everything, just dont know how to start the derivative method. Any...

by: Constantine AI 
last post by:
Hi i am having a little problem with an equation function that was created from all your help previously. The function works fine itself but with a small glitch within it.
Here is the function...

by: lllomh 
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{

by: DJRhino 
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

by: isladogs 
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...

by: giovanniandrean 
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...

by: NeoPa 
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currentlyselected (and open) object (Form or Report).
I know it can be done by selecting :...

by: NeoPa 
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...

by: Teri B 
last post by:
Hi, I have created a subform Roles. In my course form the user selects the roles assigned to the course.
0netomany. One course many roles.
Then I created a report based on the Course form and...

by: isladogs 
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...

by: isladogs 
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
 