Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Math Input
#1
Write a function area_of_circle® which returns the area of a circle of radius r

As a refresher, the area of any circle is equal to the radius squared, multiplied by pi (where pi is 3.14159....).

Don’t forget to include the math module, where pi is defined.

I have submitted:

import math
def areaOfCircle®:
return math.pi*r**2

radius = int(input("What is the radius of your cirle?"))

print(areaOfCircle(radius))


The grader says I am unable to use input statements so I am at a lost for what to use instead... any help is appreciated
Reply
#2
How to post Python code in this board.

You're not allowed to use input?

What are you allowed to use?
Arguments?
Hardcoded radius in source code?
Incomming data from a database?
Data from a rest api?

There is more than one way to feed a program with input data, but it have to be defined.

Example with arguments:
import sys
from your_sourcecode import areaOfCircle

if len(sys.argv) != 2:
    print(sys.argv[0], 'radius')
try:
    radius = float(sys.argv[1])
except ValueError:
    print('radius must be a float or integer')
    sys.exit(1)
area = areaOfCircle(radius)
print(area)
In Python you should use lower case and underscores instead of camel case for functions.
Functions should be named as verbs, because they do something.
Classes should be named with nouns, where the first letter is upper case.
It's also not always good to use single char names like r for radius.
Just name variables what they are.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Forum Jump:

User Panel Messages

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