Python Forum
Calculate area of a circle
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculate area of a circle
#1
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
Reply
#2
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...
Reply
#3
note that star imports like from math import * are generally discouraged
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
(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 ?
Reply
#5
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
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#6
(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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Hello, does anyone know how to make this script work from file by running "python nameoffile" please ?
Reply
#8
Is something not working? Please post the code inside the right tags, to add syntax highlighting, line numbering and most importantly, keep the indentation.
Reply
#9
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")
Reply
#10
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...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How Can I Draw Circle With Turtle İn Python ? mdm 2 2,751 Jun-03-2021, 02:07 PM
Last Post: DPaul
Photo I need help with a circle random color code and error Vxploit 4 3,013 Mar-21-2021, 07:23 PM
Last Post: jefsummers
  Turtle circle Fabio87 3 2,478 Dec-17-2020, 10:52 PM
Last Post: Fabio87
  Intersection of a triangle and a circle Gira 3 3,621 May-19-2019, 06:04 PM
Last Post: heiner55
  function with radius as a parameter that returns area of a circle taydeal20 4 7,184 Feb-07-2018, 03:33 PM
Last Post: buran
  radius of circle question charlottecrosland 5 4,215 Sep-11-2017, 09:50 AM
Last Post: charlottecrosland

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020