0

I am making a program that gives me trigonometric function's value.

import math

function = str(input("Enter the function: "))
angle = float(input("Enter the angle: "))
print(math.function(angle))

We have to input, say, sin(x) into the function. So we input "sin" in the variable "function" and let "angle" be "x".

Syntax of math is:

math.sin(x)

But the way I want it to happen is:

  1. Assign the value of function as "sin"
  2. Assign the value of angle as "x"
  3. Calculate the value.

I know it won't work as we are using a variable in place of a keyword. So I am looking for such a code that can use a variable and assign it to the keyword.

Jack Parkinson
  • 681
  • 11
  • 35
  • 1
    duplicates of : https://stackoverflow.com/questions/7719466/how-to-convert-a-string-to-a-function-in-python ? – HH1 Jun 28 '17 at 13:00
  • 2
    Possible duplicate of [How to convert a string to a function in python?](https://stackoverflow.com/questions/7719466/how-to-convert-a-string-to-a-function-in-python) – zaidfazil Jun 28 '17 at 13:01

3 Answers3

2

Perhaps this could work for you, using introspection, and in particular getattr(info on gettattr):

import math

function = str(input("Enter the function: "))

angle = float(input("Enter the angle: "))

# if the math module has the function, go ahead
if hasattr(math, function):
    result = getattr(math, function)(angle)

Then print result to see your answer

Totem
  • 7,189
  • 5
  • 39
  • 66
  • 1
    You may have to be careful with this, particularly if the user specifies a function which does not follow the typical format of sin/tan/etc requiring a single argument. Functions such as `hypot()` and `pow()` require two arguments but are still attributes of `math`, so depending on how robust you want to make the program you may have to account for this when using `attr()`. Just a thought. – Jack Parkinson Jun 28 '17 at 13:24
  • 1
    @ISOmetric This is true. OP you would certainly want to make sure the user can't 'break' the program by entering something unexpected... If you literally only wanted say, one of two different functions, like "sin" and "cos", then maybe one of the other answers would be more suitable.. but this is fine too if you're careful – Totem Jun 28 '17 at 13:32
2

One option is to make a dictionary of the functions you want, like so:

import math

functions = {
    'sin': math.sin,
    'cos': math.cos
}

function = functions[input('Enter the function: ')]
angle = float(input('Enter the angle: '))
print(function(angle))

Additionally, you could surround the assignment of function with a try-catch block to handle bad inputs.

Will Da Silva
  • 6,386
  • 2
  • 27
  • 52
0

Probably the simplest way to do this would be to have known statements:

import math

function = str(input("Enter the function: "))
angle = float(input("Enter the angle: "))

output = "Function not identified"  # Default output value

if function == "sin":
    output = math.sin(angle)
if function == "tan":
    output = math.tan(angle)
# Repeat with as many functions as you want to support

print output

The downside is that you have to be prepared for any input you want to allow.

Jack Parkinson
  • 681
  • 11
  • 35
  • 1
    you beat me to it :) – hridayns Jun 28 '17 at 13:03
  • "# Repeat with as many functions as you want to support" I beg to differ. Instead of 20 `if/elif` conditions needed to support 20 functions, create a dictionary from string to function: `functions_dict = {'sin': math.sin, 'tan': math.tan, ...}` then you can simply do `functions_dict[function](angle)` This of course requires all functions to accept the same arguments. – DeepSpace Jun 28 '17 at 13:06
  • I already had that one. I just wanted to make my code shorter. So, getattr is the answer. – Aditya Rana Jun 28 '17 at 13:16