Python Forum
Understanding a piece of code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Understanding a piece of code
#4
My mistake. Actually it is returning 3.14 * 10 * 10. I mixed up the x and y. That is a pretty good indication that x and y are not good variable names. They got me so confused I forgot how to calculate the area of a circle. And now my humiliation is frozen in time in Michael1's reply for all to see.

This is a better way to write the function in question. x should be renamed radius and y should be the constant pi, not a function argument. The function name should clearly indicate what the function does, and the function should have a docstring.
import math

def circle_area(radius):
    """Returns area of circle with radius 'radius' """
    return math.pi * radius**2
Reply


Messages In This Thread
Understanding a piece of code - by Michael1 - Jan-20-2022, 06:25 PM
RE: Understanding a piece of code - by deanhystad - Jan-20-2022, 06:34 PM
RE: Understanding a piece of code - by Michael1 - Jan-20-2022, 06:37 PM
RE: Understanding a piece of code - by deanhystad - Jan-20-2022, 07:11 PM
RE: Understanding a piece of code - by Michael1 - Jan-20-2022, 07:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  An unexplainable error in .format statement - but only in a larger piece of code? ToniE 4 1,758 Sep-05-2023, 12:50 PM
Last Post: ToniE
  First piece of code Bearwulf 2 1,526 Aug-02-2023, 04:03 PM
Last Post: deanhystad
  Code understanding: Need help in understanding dictionary code jt123 0 1,003 Jul-09-2023, 01:13 PM
Last Post: jt123
  New to python/coding Need help on Understanding why this code isn't working. Thanks! mat3372 8 3,785 May-09-2023, 08:47 AM
Last Post: buran
  How to make this piece concise leoahum 0 1,842 Sep-23-2021, 09:23 PM
Last Post: leoahum
  .maketrans() - a piece of code which needs some explanation InputOutput007 5 4,436 Jan-28-2021, 05:05 PM
Last Post: buran
  Beginner: I need help understanding few lines of a code. hop_090 1 2,327 Sep-07-2020, 04:02 PM
Last Post: Larz60+
  Extracting Rows From Data Frame and Understanding The Code JoeDainton123 0 1,966 Aug-03-2020, 04:08 PM
Last Post: JoeDainton123
  I am a newbie.Help me with this simple piece of code feynarun 3 3,844 Jan-08-2020, 12:40 PM
Last Post: perfringo
  How can I improve this piece of code? aquerci 3 3,191 Nov-17-2019, 10:57 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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