Answer the question
In order to leave comments, you need to log in
How to call a variable with string data type?
The user enters a function, and then I work with it. Python treats the value "y" as a dumb string, not something you can work with.
y = input( ‘Введите функцию’ )
x = input( ‘Введите аргумент (x)’ )
answer = y(x)
Answer the question
In order to leave comments, you need to log in
You need to get the function by name
import sys
this_module = sys.modules[__name__]
def square(x):
return x * x
print(getattr(this_module, 'square')(10))
Foo
Process finished with exit code 0
def square(x):
return x * x
functions = {
'square': square
}
print(functions['square'](10))
100
Process finished with exit code 0
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question