Help with Python exercise

Solved
Blast3000 -  
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:
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

Phil_1857 Posted messages 1883 Registration date   Status Member Last intervention   169
 
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)))
1
Anonymous user
 
Thank you very much, I better understand the logic of Python.
0
Phil_1857 Posted messages 1883 Registration date   Status Member Last intervention   169 > Anonymous user
 
you just have to show us your finalized code...
0
Anonymous user > Phil_1857 Posted messages 1883 Registration date   Status Member Last intervention  
 
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)))
0
Phil_1857 Posted messages 1883 Registration date   Status Member Last intervention   169 > Anonymous user
 
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)))
1
Blast3000 > Phil_1857 Posted messages 1883 Registration date   Status Member Last intervention  
 
Thank you for taking the time to explain things to me.
0
Phil_1857 Posted messages 1883 Registration date   Status Member Last intervention   169
 
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...
0
Blast3000
 
Thank you, I corrected the mistake. Is it okay to do the calculation that I am asked to do?
0
Phil_1857 Posted messages 1883 Registration date   Status Member Last intervention   169
 
I don't know: you are not showing your corrected code...
0
Anonymous user
 
l'instruction :
the code :
x=int(input("choose a real number: ")) x=1/2*x**2+5*x-4 print("the result is: ", x)
0
baladur13 Posted messages 47296 Registration date   Status Moderator Last intervention   14 382
 
Hello,
Regardless of the use of int() for a floating point which is a bit weird,

You ask to enter a number x and right away you display x= a value different from the one entered???
On line 2 I think it should be y= f(x)
For me the correct answer is in the form y = f(x) = result
0
Phil_1857 Posted messages 1883 Registration date   Status Member Last intervention   169 > baladur13 Posted messages 47296 Registration date   Status Moderator Last intervention  
 
that 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...
0
Anonymous user > baladur13 Posted messages 47296 Registration date   Status Moderator Last intervention  
 
Hello,
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?
0
baladur13 Posted messages 47296 Registration date   Status Moderator Last intervention   14 382 > Anonymous user
 
y=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)
1
Phil_1857 Posted messages 1883 Registration date   Status Member Last intervention   169
 
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
0
Anonymous user
 
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)
0