-1

For example, if the input is "a" then run a function and if "b" then another function. The only reason that I am doing this is that I have a really long code with lots of functions What I was thinking was this.

userinput = input("Input:")

def a():
    print("You entered A")
def b():
    print("You entered B")
if userinput == "a":
    a()
if userinput =="b":
    b()

This is what I would normally do but it would be too long. The code that I am doing will have many functions so it would be inefficient. I am trying to find a code that will not use a lot of if functions. My code would have over a hundred functions according to what the user has entered. This is just an example to show the functions. I was thinking of this.

userinput = input("Input:")
def a():
    print("You entered A")
def b():
    print("You entered B")

[userinput]()#I did input in the bracket so if someone enters "a" for example then it would run function a and if "b" then it runs function b

This is what I want to do but it doesn't work. If anyone could help me I would be very thankful. All the other ones I looked for did not have any alternatives other than using if else.

Coozywana
  • 27
  • 10
  • What if you considered using a match case statement instead? While what you want to do is possible, it's rather messy to do so, because it involves directly accessing the `globals()`/`locals()` dictionaries and testing if your key is in them, etc – iteratedwalls Sep 19 '22 at 03:06
  • Use a dispatch dictionary: `dispatch = {'a': a, 'b': b}` / `val = input()` / `dispatch[val]()`. – Tim Roberts Sep 19 '22 at 03:44
  • @TimRoberts Hello, thank you, could you please show me how to use the dispatch dictionary? – Coozywana Sep 19 '22 at 05:39
  • I just did. That's what my reply is showing. – Tim Roberts Sep 19 '22 at 06:18
  • Your edit does not seem to invalidate the duplicates; in fact, quite the opposite. In brief, to map the input `"a"` to `afunc()` and `"b"` to `froombozzle()`, you'd use a dictionary `{"a": afunc, "b": froombozzle}`. Notice the absence of parentheses; you'd then use `yourdict["a"]()` to run `afunc()`, for example. – tripleee Sep 19 '22 at 06:28
  • I have no idea what a dictionary is – Coozywana Sep 19 '22 at 06:56
  • The one I ticked actually answers my question properly. He is actually smart. Also, why do duplicates matter? All that matters is people getting their answers. – Coozywana Sep 19 '22 at 06:56

2 Answers2

0

First of all, you are doing a HUGE mistake assigning input = input("Input:"). You are reassigning a built-in function to something different, so your code is going to break if you need to call the built-in function input again. Here you can find examples of how to define variables https://www.w3schools.com/python/gloss_python_variable_names.asp.

So for your code, maybe you can try something like this:

user_input = input("Input:")

def user_answer(user_input):
    if user_input.upper() == "A":
        print("You entered A")
    elif user_input.upper() == "B":
        print("You entered B")
    else:
        print(f"You print: {user_input}")

user_answer(user_input)
0
def a():
    print ('call sucess')

x = input('input function :')

try:
    globals()[x]()
except Exception as e:
    print (x, 'is not exists function')