I am new to the programming and I am getting familiar with python version 3.4.
I am not that good at math and I am struggling with a question and it goes like this:
Write a program that asks the user an integer ‘x’ and prints the value of y after evaluating the following:
y=x² - 12x+11
I don’t know how to do it and I certainly don’t the formula for it. I want to create a program for it in python 3.4. Can some one give me in depth explanation. Thanks in advance.
Hello, @corejumper08341!
The code is simple, you’ll see.
You’ll need to get the value of X from the user so a simple input for that will do the job.
Now to the most important part: the calculation.
If the same function, y, was given to you in real life, how would you do it?
How would you write it down?
The same way you’ll write it down in real life, you’ll do it in code but with a few changes:
y = x * x - 12 * x + 11
So let’s see: all you’ll need to do now is to write down the line above and print the value of y because the “=” is used to assign a certain value into something.
You can test, try the following:
print y #None because nothing was stored inside of this variable.
y = x * x - 12 * x + 11
print y #Now you'll get a different result from None.
I’d suggest you to check the math library in Python, it will be really helpful for you!
Quote me if there’s anything else I can help you with!
Best regards,
g4be
Edit: mathematical operations in coding follow the rules we use while handling with that in real life.
@corejumper08341: The value you used for the example gave the correct answer which is -9 but if the output must be -16, try it with x = 3, you’ll get the desired output.
If you want to find another specific value, simply use delta (Δ = b * b - 4 * a * c) and bhaskara (x1 = (-b + sqrt(Δ))/2a and x2 = (-b - sqrt(Δ))/2a ).
Quote me if there’s anything else I can help you with!
I know the correct answer is -9 sorry never mind -16.
the question, is am I doing the formula right? because the response from exercise checker is marking me wrong. I think the steps I am taking are wrong that’s why is not accepting it. I am putting this into idle and transfer the code to the exercise checker but is not accepting it. perhaps the steps of the formula are wrong, is there another way of doing it?? Thanks again.
You can total write your equation directly into python, the only issues you will ever run into is if you use floats as they cannot be represented exactly in binary.
Python:
def function1(x):
return x ** 2 - 12 * x + 11
Python understands the PEMDAS so it will always do the correct order unless certain things happen.
If this [quote=“corejumper08341, post:1, topic:24492”]
y=x² - 12x+11
[/quote]
is the correct formula then the output of your function is correct.
Are you still getting errors?
I think I get it what’s wrong.
The value -16 is just an example that was given, it’s not an obligation to be the output.
Get the value of X from the user by using the function raw_input(“Enter a value”), like this:
x = raw_input("Enter a value:")
The user will type and you’ll get the value but it will be a string so use the function int() passing your variable X as an argument and store that into “X” again.
After that, you just need to calculate and store the calculation result into Y and print it out.
That’s exactly what the exercise is telling you but somehow I got confused on the -16 result, my apologies.
Edit: I’d suggest you to use repl.it - Python* to check your code by executing it.