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.

2

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
@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
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.

Comments

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.

3 Comments

you beat me to it :)
"# 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.
I already had that one. I just wanted to make my code shorter. So, getattr is the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.