""" Do single parameter math calculations with prompting
optionally converting degrees to radians"""
import math
try:
input = raw_input
# Python 2, input is replaced by raw_input like in Python 3
except:
# no raw_input -> Python 3, input has correct definition -> do nothing
pass
lookup = dict((func,math.__dict__[func])
for func in math.__dict__
# let's take out special __ starting functions
if not func.startswith('__'))
while True:
a = input("""
Enter function or
empty input to quit
? for list of functions:
""").lower()
try:
func = lookup[a]
except KeyError:
if not a:
break
elif a == '?':
print('Available functions are:\n\t'+'\n\t'.join(lookup)+'\n')
else:
print("%r is not a legal function choice!" % a)
else:
if hasattr(func, '__call__'):
print('\n%s\n' % func.__doc__)
try:
args = input('Give parameter for %s: ' % a)
if args:
args = args.split(',')
args = [float(arg) if '.' in arg else int(arg) for arg in args]
if 'radians' in func.__doc__:
args = args if input('Convert degrees to radians (Y/n)?').lower().startswith('n') else map(math.radians, args)
print (func(*args))
else:
# function without parameters
print(func())
except ValueError as e:
print(e)
except TypeError:
print('Wrong number of parameters for %s' % a)
else:
print('Variable %r has value %s' % ( a, func))