I'm writing a practice script to prompt the user to input "Area" or "Radius", then input the area or radius of a circle to calculate the other.
I wanted to use functions to do it.
Here's what I've got so far:
import math
from math import pi
def area():
radius = float(input("To calculate the Area enter the Radius of the circle: "))
area = float(pi)*pow(radius,2)
print("The Area is " + str(area))
def radius():
area = float(input("To calculate the Radius enter the Area of the circle: "))/pi
radius = str(math.sqrt(area))
print("The Radius is " + radius)
choice = {'Radius': radius, 'Area': area}
action = input("Calculate Area or Radius? ")
action = action.capitalize()
choice[action]()
Is there a superior way to do this? Are there any lines I can cut or condense?
Superior would be functions with parameters and return values, which are far more versatile. I also like if __name__ blocks. There are very often lines to cut, but that doesn't mean you want to cut them. You could do:
import math
def area(radius):
return math.pi * radius ** 2
def radius(area):
return math.sqrt(area / math.pi)
if __name__ == '__main__':
choices = {'Radius': (radius, 'Radius', 'Area'), 'Area': (area, 'Area', 'Radius')}
action, calculate, given = choices[input("Calculate Area or Radius? ").capitalize()]
result = action(float(input('To calculate the {}, input the {}: '.format(calculate, given))))
print('The {} is {:.2f}.'.format(calculate, result))
But that is perhaps excessive.
Here is a variation on the same concept. It is longer but there a richer parsing of user input and an extended functionality
from math import pi, sqrt
class Interaction:
def ask_parameter(self):
prompt = """\
Please enter one of the circle's parameter. You may enter
"radius <number>" or "circumference <number>"
or "diameter <number>" or "area <number>". You may also
use r, c, d, a to abbreviate these words.
Your choice: """
while True:
reply = input(prompt).strip().split()
if len(reply) != 2:
self.say_invalid_input()
continue
param = reply[0].lower()
if not param in [
'radius', 'circumference', 'diameter', 'area',
'r', 'c', 'd', 'a']:
self.say_invalid_input()
continue
param = param[0]
try:
value = float(reply[1])
except ValueError:
self.say_invalid_input()
continue
else:
if value < 0:
print('ERROR: value must be non negative, try again.')
continue
if param == 'd':
self.r = value / 2
elif param == 'a':
self.r = sqrt(value / pi)
elif param == 'c':
self.r = value / (2 * pi)
else:
self.r = value
return
def display_results(self):
print("""\
area = {a}
circumference = {c}
diameter = {d}
radius = {r}""".format(
r = repr(self.r),
a = repr(pi * self.r ** 2),
c = repr(self.r * 2 * pi),
d = repr(self.r * 2)
))
def say_invalid_input(self):
print('ERROR: invalid input, try again.')
def run(self):
self.ask_parameter()
self.display_results()
if __name__ == '__main__':
Interaction().run()
Example in a console
Output:
λ python3 paillasse/circle.py
Please enter one of the circle's parameter. You may enter
"radius <number>" or "circumference <number>"
or "diameter <number>" or "area <number>". You may also
use r, c, d, a to abbreviate these words.
Your choice: diameter 23
area = 415.4756284372501
circumference = 72.25663103256524
diameter = 23.0
radius = 11.5
Possible improvements could be to enter parameter by an option on the command line instead of prompting user, or on the contrary to start an interactive interpreter allowing the user to analyse several circles in a session for example with module cmd, or use a GUI.