0

I'm supposed to write a script that asks for the radius of a circle and solves it for the area. I've tried googling it but I've all I've gotten are answers with import somewhere in the script or some really complex stuff that I don't understand and I'm not going to copy paste because I wouldn't learn anything from that.

print("What is the radius?")
radius=input()
def PI 3.14159
area=PI*radius**2
print("The area of the circle is",area)
Attie
  • 6,690
  • 2
  • 24
  • 34
Mike
  • 21
  • 2
    I'm not sure what you're asking. What's wrong with defining a variable like `pi = 3.14159`? – pault Jun 07 '18 at 19:28

1 Answers1

4

Variable assignments in python are done like so:

PI = 3.14158

You don't need do declare the variable or its type.

You can't declare a constant in python, so we rely on using capitals to denote "do not change this value". This is laid out in PEP-8, the "Style Guide for Python Code":

Constants are usually defined on a module level and written in all capital letters with underscores separating words. Examples include MAX_OVERFLOW and TOTAL.


def is used to introduce a function, for example:

def pi():
    return 3.14158

You could then use the returned value like this:

area = pi() * radius**2
Attie
  • 6,690
  • 2
  • 24
  • 34
  • 2
    Yeah, but they do it correct ***on the next line***... – Makoto Jun 07 '18 at 19:29
  • i'd use PI instead of pi. I think its common to capitalize constants – Grant Williams Jun 07 '18 at 19:30
  • 1
    Yep, strange huh. It's worth giving people who are new to writing code a helping hand though, and expanding on what `def` might do... – Attie Jun 07 '18 at 19:30
  • @GrantWilliams done – Attie Jun 07 '18 at 19:34
  • What other feedback where you looking for on the downvote? Thought mine was tacit enough there... – Makoto Jun 07 '18 at 19:39
  • Huh. I don't think that attitude is helpful or welcoming for beginners - many languages have a keyword for constants, so I thought the question was fair enough... though admittedly a duplicate, I tried to add more useful info to my answer. – Attie Jun 07 '18 at 19:47