Help with Python exercise
Solved
Hello,
I have an exercise to do in Python, I've done it but I'm not sure if that's really what I was supposed to do. Could you tell me if I understood the task correctly?
The question is: Write a Python program that asks the user to input a real number x and returns the value of 1/2*x (squared) + 5x - 4 and provide this algorithm in Python.
Here is what I was able to do:
Everything works well, but I wanted to know if that's what was asked of me?
EDIT: Added the LANGUAGE in the code tags (syntax highlighting).
Explanations available here:
https://codes-sources.commentcamarche.net/faq/10686-le-nouveau-codes-sources-comment-ca-marche#balises-code
Thank you for keeping this in mind in your future messages.
I have an exercise to do in Python, I've done it but I'm not sure if that's really what I was supposed to do. Could you tell me if I understood the task correctly?
The question is: Write a Python program that asks the user to input a real number x and returns the value of 1/2*x (squared) + 5x - 4 and provide this algorithm in Python.
Here is what I was able to do:
x=int(input(´´choose an integer number:´´)) x=1/2*x**2+5*x-4 print(´´the result of the calculation is:´´, x)
Everything works well, but I wanted to know if that's what was asked of me?
EDIT: Added the LANGUAGE in the code tags (syntax highlighting).
Explanations available here:
https://codes-sources.commentcamarche.net/faq/10686-le-nouveau-codes-sources-comment-ca-marche#balises-code
Thank you for keeping this in mind in your future messages.
7 answers
-
It's good this time
As for y=f(x), it's true that it would be better to define a function for that:
def f(x):
that contains the calculation (y = ....) and returns the value y
then you can write:
print('For x = {} , ½x²+5x-4 = {}'.format(x, f(x)))-
-
@Anonymous useryou just have to show us your finalized code...
-
@Phil_1857
x=float(input("choose a real number: ")) def f(x) : y=1/2*x**2+5*x-4 return y print('For x = {} , ½x²+5x-4 = {}'.format(x, f(x))) -
@Anonymous userHello,
Very good!
However, you need to learn to use code tags to display your program
(see the link provided by Baladur above)
after you validate, it should look like this:#in principle, we put the functions at the beginning... def f(x) : y=1/2*x**2+5*x-4 return y #... then the main program x=float(input("choose a real number: ")) print('For x = {} , ½x²+5x-4 = {}'.format(x, f(x))) -
@Phil_1857Thank you for taking the time to explain things to me.
-
-
Hello,
to know if that’s really what is asked of me to do?
First, you are asked to enter a real number, and you write:
x=int(input('choose an integer:'))
so you are entering an integer, not a real number... -
Thank you, I corrected the mistake. Is it okay to do the calculation that I am asked to do?
-
I don't know: you are not showing your corrected code...
-
-
-
@baladur13that he should already fix the int problem, since apparently it doesn't seem obvious to him
then, in a second step, your remark is pertinent... -
@baladur13Hello,
I corrected:x=float(input("choose a real number: "))
f=1/2*x**2+5*x-4
print("the result is: ", f)
I would like to understand y=f(x) for line 2, can you please explain it to me? -
Moderator@Anonymous usery=f(x) denotes that y is a function of x.
The function being ½x²+5x-4
For better display I would prefer
Print ("For x =", x)
Print ("½x²+5x-4 =", f)
-
-
That's exactly what I thought: you haven't corrected anything at all!
x=int(input(.....))
with int() you convert the input string into an integer not a float
so, you cannot enter a number like 12.563 -
I'm sorry, I'm trying to understand I'm a beginner
I think the correct code would be:x=float(input("choose a real number: "))
f=1/2*x**2+5*x-4
print("the result is:", f)
