-1

Ive search the internet for my question sadly, i couldn't find any answer. here is the code.

print('a. sugar b. tomato c. honey d. all')

def a_func():
    print("sugar")

def b_func():
    print("tomato")

def c_func():
    print("honey")

func_map = {'a': a_func, 'b':b_func, 'c':c_func 'd':???? }

while True:
    func_input = input("Enter a letter 'a' through 'd'!\n")

    if func_input.strip() == 'exit':
        print('goodbye! ')
        exit()

    if func_input.strip() in func_map.keys():
        func_map[func_input]()
    else:
        print("Sorry no function for that!")

Now, I want to call all of the functions

If i enter d

The output will be like this.

  sugar
  tomato
  honey

Thank you in advance!

kuro
  • 3,214
  • 3
  • 15
  • 31

4 Answers4

2

Define a function that calls all the elements in a loop, and put that in the d element.

def all_func():
    for f in func_map.values():
        if f != all_func: # prevent infinite recursion
            f()

func_map = {'a': a_func, 'b':b_func, 'c':c_func 'd':all_func }
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Simply create one more function as d_func and call all other functions in that.

def d_func():
    a_func()
    b_func()
    c_func()

func_map = {'a': a_func, 'b':b_func, 'c':c_func, 'd':d_func }
Avinash
  • 875
  • 6
  • 20
0
print('a. sugar b. tomato c. honey d. all')
def a_func():
   return "sugar"
def b_func():
   return("tomato")
def c_func():
    return("honey")
func_map = {'a': a_func(), 'b':b_func(),'c':c_func(),'d':a_func()+'\n'+b_func()+'\n'+c_func() }
while True:
    func_input = input("Enter a letter 'a' through 'd'!\n")
    if func_input.strip() == 'exit':
        print('goodbye! ')
        exit()
   elif func_input.strip() in func_map.keys():
        print(func_map[func_input])
   else:
       print("Sorry no function for that!")
zeeshan12396
  • 382
  • 1
  • 8
0

You can also use a lambda to call all functions at once with exec:

func_map = {'a': a_func, 'b':b_func, 'c':c_func, 'd': lambda:exec("a_func()\nb_func()\nc_func()")}

Also see:

python compile for exec

https://stackoverflow.com/a/9547687/16136190

The Amateur Coder
  • 789
  • 3
  • 11
  • 33