Python Forum
Understanding a piece of code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Understanding a piece of code
#1
def area(x,y = 3.14): # formal parameters
    a = y*x*x
    print (a)
    return a

a = area(10)
print("area",a)
* Here is what I am doing:
3.14 * 3.14 * 3.14 = 30.56476
30.56476 is a
then 30.56476 * 10
305.6476

but the actual answer is: 314.0 ( when you run the program)
buran write Jan-20-2022, 06:48 PM:
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
Reply
#2
Actually you are doing 10 * 3.14* 3.14 and the answer should be 98.596.

This is bad code. What does it do? area? area of what? By looking at the code I can see that this is the area of a circle with radius x, but I should not have to read the code. And why is y a variable? If this computes the area of a circle is y ever going to be something other than PI?
def circle_area(radius):
    """Returns area of circle with radius 'radius'"""
    return 3.14 * radius * radius
What is 3.14? It is kind of close to PI. If it is supposed to be PI, why not use PI?
import math

def circle_area(radius):
    """Returns area of circle with radius 'radius'"""
    return math.pi * radius**2
Reply
#3
Im trying to understand why the complier gives 314.0
Reply
#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
#5
Thanks!
Reply


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 656 Sep-05-2023, 12:50 PM
Last Post: ToniE
  First piece of code Bearwulf 2 723 Aug-02-2023, 04:03 PM
Last Post: deanhystad
  Code understanding: Need help in understanding dictionary code jt123 0 450 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 1,661 May-09-2023, 08:47 AM
Last Post: buran
  How to make this piece concise leoahum 0 1,299 Sep-23-2021, 09:23 PM
Last Post: leoahum
  .maketrans() - a piece of code which needs some explanation InputOutput007 5 2,898 Jan-28-2021, 05:05 PM
Last Post: buran
  Beginner: I need help understanding few lines of a code. hop_090 1 1,642 Sep-07-2020, 04:02 PM
Last Post: Larz60+
  Extracting Rows From Data Frame and Understanding The Code JoeDainton123 0 1,405 Aug-03-2020, 04:08 PM
Last Post: JoeDainton123
  I am a newbie.Help me with this simple piece of code feynarun 3 2,745 Jan-08-2020, 12:40 PM
Last Post: perfringo
  How can I improve this piece of code? aquerci 3 2,167 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