With this code you can explore the functions of math module interactively seeing the docstrings automatically for each function.

EDIT: The docstring was unfortunately from old code, this version copes with values like 'e' and multiple parameter functions.

Edited by pyTony: n/a

``````""" 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))``````

Specialties:
IT/Science/Contracts/Religious translation/interpreting FIN-ENG-FIN
Python programming

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by pyTony

Lmao, this is the snippet of the code you posted in my Combining if statements thread xDDDD

It is adapted to include multiple parameters and checking of when radians conversion is apropriate from docstring, among other improvements. Little hurried it through as you can see prompting for parameter instead of 'appropriate number of parameters' and the old docstring in beginning, like I wrote.

It is good idea to finalize the code for some responses (maybe generalize little) and post them as code snippets if they could be helpful for somebody in future.

I wanted to post some new code for the holidays and to celebrate 2000 posts mark, which does not show actually in post count in my postings, but you can see in my info page. For some reason they are out of sync.

Edited by pyTony: n/a

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.