-1

I have used the following code:

from time import time
import datetime

now = datetime.datetime.now()
Report = now.strftime("%Y%m%d_%H%M%S") + "Prueba llamada de funciones" + ".txt"
modo=0



def llamar_funciones():
    if (modo==1):
        Alimentaciones_1(Report)
        Alimentaciones_2(Report)
    else:
        print("¿Que ensayos de alimentaciones quieres hacer?")
        Ensayo=input()
        Nombre_Ensayo= "Alimentaciones_" + Ensayo
        print(Nombre_Ensayo)
        Nombre_Ensayo(Report)
def Alimentaciones_1(Report):
    f = open(Report,"a")
    f.write("1.funtzioa")
    f.close()
def Alimentaciones_2(Report):
    f = open(Report,"a")
    f.write("\n2.funtzioa")
    f.close()
    
llamar_funciones()

I get the following error:

TypeError: 'str' object is not callable

How can I call the function 1 or 2 depending on the value of the variable "modo"??

xabi
  • 13
  • 3
  • When posting a question about code that produces an Exception, always include the complete Traceback - copy and paste it then format it as code (select it and type `ctrl-k`) – wwii Feb 07 '21 at 17:06
  • What do you expect `Nombre_Ensayo(Report)` when `Nombre_Ensayo` is a string? You can only call functions, not strings... – trincot Feb 07 '21 at 17:07
  • The error is that `Nombre_Ensayo` is a string, and you are trying to use it like a function in `Nombre_Ensayo(Report)`. I have no idea what you are trying to do here, though. – mkrieger1 Feb 07 '21 at 17:07
  • you need to use if statements – SdahlSean Feb 07 '21 at 17:07
  • What I am trying to do is to select the function Alimentaciones_1 or Alimentaciones_2 depending on the input in case the variable "modo" is not 1. In this case the input should be 1 or 2 so that I can choose Alimentaciones_1 or Alimentaciones_2 – xabi Feb 07 '21 at 17:11
  • put the functions in a list or dict and call them by index – user8408080 Feb 07 '21 at 17:12

2 Answers2

0

You simply need another if statement. You can't call strings, but you can branch based on them.

if Ensayo == "1":
    Alimentaciones_1(Report)
elif Ensayo == "2":
    Alimentaciones_2(Report)
else:
    # ... Handle invalid user input here ...

For what it's worth, you can call a function whose name is a string, but it's poor coding style and extremely bug-prone. To be clear, the following is for educational purposes; do not do it. The above snippet is the accepted way to do this sort of thing.

eval("Alimentaciones_{}".format(Ensayo))(Report) # <-- Bad idea
Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
0

Python has two functions that might help you, exec() and eval(). They both take a string and treat it like python code (you could use the string from the input and run it), and behave slightly differently.

However, and this is important, don't use them. At all. Those functions are extremely dangerous, as they let the user call every function from your code if they wish, and it's bad practice.

I recommend to use if cases to check for the values you want like:

if my_str == "my_func":
    my_func(...)
elif my_str == "my_other_func":
    my_other_func(...)
...

This might be tedious but it will let your users call whatever they need to and not more.

Nimrod Rappaport
  • 134
  • 1
  • 1
  • 12