Can some one help me please?

python_3

#1

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.


#2

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.


#3

I did it this way
x=10
x*x-12*x+11
-9

but the correct answer should be -16
please tell me what I am doing wrong??


#4

@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(Δ))/2*a and x2 = (-b - sqrt(Δ))/2*a ).

Quote me if there's anything else I can help you with!

Best regards,
g4be


#5

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.


#6

So your code is supposed to print out the result of the function you posted?
Mind if you post the code and the question of the exercise?


#7

@corejumper08341

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

is the correct formula then the output of your function is correct.


#8

The question is: Write a program that asks the user an integer 'x' and prints the value of y after evaluating the following: y=x² - 12x+11

And this what I get from the exercise checker:

Your answer is NOT CORRECT
For example, when user input is: 3

####### Your printed output
####### Correct output should be

-16


#9

Ok lets do this by hand then and see if we get the same thing.

y=x² - 12x+11
IF        THEN
x = 0 y = 11
x = 1 y = 0
x = 2 y = -9
x = 3 y = -16

Now let's see what our function gives us.

def function1(x):
    return x ** 2 - 12 * x + 11

IF        THEN
x = 0 y = 11
x = 1 y = 0
x = 2 y = -9
x = 3 y = -16

Looks correct to me, in your checker put this code and tell me what it gives you.

def dumpArgs(func):
    '''Decorator to print function call details - parameters names and effective values'''
    def wrapper(*func_args, **func_kwargs):
        arg_names = func.__code__.co_varnames[:func.__code__.co_argcount]
        args = func_args[:len(arg_names)]
        defaults = func.__defaults__ or ()
        args = args + defaults[len(defaults) - (func.__code__.co_argcount - len(args)):]
        params = list(zip(arg_names, args))
        args = func_args[len(arg_names):]
        if args: params.append(('args', args))
        if func_kwargs: params.append(('kwargs', func_kwargs))
        print(func.__name__ + ' (' + ', '.join('%s = %r' % p for p in params) + ' )')
        return func(*func_args, **func_kwargs)
    return wrapper

@dumpArgs
def function1(x):
    return x ** 2 - 12 * x + 11

Then just tell what it prints out. We will then be able to solve what is going on quickly.


#10

Are you storing the value 3 in "X" and using for the calculation?
All you'd need to do is write it in a "programming" way, just like I posted earlier.

Also, make sure to use print to print the value of it.


#11

thanks I ton to all of your support but I still didn't get the right output. Thanks again.


#12

@corejumper08341: Could you take a screenshot of it so we could take a look?


#13


#14

You need to assign the value you get after executing the entire operation into "X" or any other variable you'd like.
Example:

y = 3
y = y * 2 + y - 5
print y

Also writing -16 there won't work so I think you should remove that.

Try to do that and let me know if it's works :slightly_smiling:


#15

sorry but how did you come with
y = 3
y= y * 2 + y - y?

and what I will do with the x² formula? Should I do the same formula and if I do I will still get the -16 on that


#16

The Y and the other information about it were just an example.
Whenever you have something at the power, for example, two we can simply do:

X ** 2 # This is the equivalent of X²

We'll use the double asterisk to do it.

Get the formula the exercise gave it to you, convert that formula into code and assign the value 3 to your variable X.

#This is how you assign a value to a variable
x = 3

Try to use the example I gave to you, the one involving Y, and see if it works.


#17

I did what you exactly told but still no luck, anyways thank you very much for your help and thanks for putting up with my questions .


#18

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.


#19

it is alright my friend, I handed in my exercise and is marked wrong . I am just going to wait for the answer. Thank you very much for your support.


#21