Help with Python exercise
Solved
Blast3000
-
Phil_1857 Posted messages 1883 Registration date Status Member Last intervention -
Phil_1857 Posted messages 1883 Registration date Status Member Last intervention -
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)))-
-
-
-
Hello,
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))) -
-
-
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...
-
-
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)
