Posts: 14
Threads: 5
Joined: Mar 2020
Mar-21-2020, 04:05 PM
(This post was last modified: Mar-21-2020, 04:10 PM by buran.)
Hello everybody, I'm a beginner in python, I just started today with it, and I created the following file in Linux :
cat fonction.py
#!/usr/bin/python
from math import*
def aire(rayon):
A=pi*rayon**2
return(A) Nothing happened when I ran from the terminal the following commands : python fonction.py
What is missing ? And how to make it work by launching python fonction.py
The goal is to calculate the area of a circle
Thank you
Posts: 1,822
Threads: 2
Joined: Apr 2017
All you've done is define the function. You need to call it somewhere, supplying a value for the parameter rayon . You'll probably also want to print the value that the function returns...
Posts: 8,090
Threads: 154
Joined: Sep 2016
Mar-21-2020, 04:11 PM
(This post was last modified: Mar-21-2020, 04:11 PM by buran.)
note that star imports like from math import * are generally discouraged
Posts: 14
Threads: 5
Joined: Mar 2020
Mar-21-2020, 04:42 PM
(This post was last modified: Mar-21-2020, 04:59 PM by pythonuser1.)
(Mar-21-2020, 04:08 PM)ndc85430 Wrote: All you've done is define the function. You need to call it somewhere, supplying a value for the parameter rayon . You'll probably also want to print the value that the function returns...
Hello, thank you, like this ?
#!/usr/bin/python
from math import *
def aire(rayon):
A=pi*rayon**2
rayon=5
return(A)
print(A)
I still have nothing when I run python fonction.py
(Mar-21-2020, 04:11 PM)buran Wrote: note that star imports like from math import * are generally discouraged
Hello, thank you, how do we know by what should we replace the star ? Is there a list somewhere ?
Posts: 354
Threads: 13
Joined: Mar 2020
Personally, I would prefer not using functions unless needed
So, instead of using the function, you can ask the user for the radius or put a radius
Then calculate it using the formula
Posts: 8,090
Threads: 154
Joined: Sep 2016
Mar-22-2020, 10:36 AM
(This post was last modified: Mar-22-2020, 10:36 AM by buran.)
(Mar-22-2020, 10:23 AM)pyzyx3qwerty Wrote: Personally, I would prefer not using functions unless needed Please, don't advise such things. It's a bad advise. This will work for small scripts. Yeah, this qualify for 'small script', but it's better to learn how to structure code in small reusable building blocks that are easier to test and maintain. I think that is exactly what OP does, i.e. they don't have major problem with their code (i.e. there are things like the names they use, unnecessary brackets, star import, etc), but the code is runnable.
Posts: 14
Threads: 5
Joined: Mar 2020
Hello, does anyone know how to make this script work from file by running "python nameoffile" please ?
Posts: 1,822
Threads: 2
Joined: Apr 2017
Is something not working? Please post the code inside the right tags, to add syntax highlighting, line numbering and most importantly, keep the indentation.
Posts: 14
Threads: 5
Joined: Mar 2020
Mar-22-2020, 09:58 PM
(This post was last modified: Mar-22-2020, 09:59 PM by pythonuser1.)
I added the parameter rayon and the print at the end like you said but still not working, I don't know why
cat fonction.py
#!/usr/bin/python3
from math import *
def aire(rayon):
A=pi*rayon**2
rayon=5
return(A)
print("A")
Posts: 1,345
Threads: 2
Joined: May 2019
You aren't calling the function.
Change line 7 to print(aire(5))
Line 5 defines rayon a little late. You are passing it to the function anyway...
|