Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Python is it possible to call multiple functions from the same line depending on previous events?

richodude

New Coder
Python:
import random

a = [1,2,3]
random.shuffle(a)

print(a)
print(a[0])

def fun1():
    print("I'm function 1!")
    
def fun2():
    print("I'm function 2!")
    
def fun3():
    print("I'm function 3!")
    
fun(a[0])

This is a simplified version of a deck of cards hence the odd random selection. Is there a way to modify the code above in order to get my expected outcome? (if a == 2 then fun2() would be called)

Python:
def deal():
    global p_hand
    global d_hand
    p_hand = deck[0] + deck[1]
    d_hand = deck[2] + deck[3]
    print("player's" ,deck[0],deck[1], "vs dealer's" ,deck[2] )
    if d_hand == 21:
        elim()
    if deck[2] == 2:
        d2()
    if deck[2] == 3:
        d3()
    if deck[2] == 4:
        d4()
    if deck[2] == 5:
        d5()
    if deck[2] == 6:
        d6()
    if deck[2] == 7:
        d7()
    if deck[2] == 8:
        d8()
    if deck[2] == 9:
        d9()
    if deck[2] == 10:
        d10()
    if deck[2] == 11:
        d11()

This is my current solution which seems quite long and inelegant
 
Could try using a dict. Would get rid of all the ifs.
Maybe something like this.

Python:
from random import randint

# Define the functions
def func1():
    return 'This is function 1'

def func2():
    return 'This is function 2'

def func3():
    return 'This is function 3'

# Create a dict - numbers are key functions are values
functions = {
    1: func1(),
    2: func2(),
    3: func3()
}

# Create a random number from 1 to 4. If 4 is picked the else will fire.
number = randint(1, 4)

# Check the dict keys for the number and print
if number in functions.keys():
    print(functions[number])
else:
    print('That number does not exist in the dict.')

output

Code:
This is function 1

On a side note: It is generally bad practice to put globals in a function. You should pass them as arguments or some other form to use them.
 
Last edited:
You can also do this:
Python:
from random import randint

# Define the functions
def func_1():
    return 'This is function 1'

def func_2():
    return 'This is function 2'

def func_3():
    return 'This is function 3'

# Create a random number from 1 to 4. If 4 is picked the else will fire.
num = randint(1, 4)

# Check the locals dict for the number and print
if f"func_{num}" in locals():
    print(locals()[f"func_{num}"]())
else:
    print('That number does not exist.')
 
What you are asking for is dynamic functions. See this article for a number of ways to dynamically call a function. The first option is the easiest (if perhaps not the best in terms of security). Using this method you could replace your series of if statements on deck[2] by this code

Python:
func = globals()["d"+str(deck[2])]
func()

which will dynamically call one of d2..d11 according to the value of deck[2].

But do you really need 10 different functions to handle the different values of deck[2] ? I have a hunch these functions will look very much alike, with a lot of redundancy between them. Would it not be better to have just one function with the number 2..11 as parameter ?
 
What you are asking for is dynamic functions. See this article for a number of ways to dynamically call a function. The first option is the easiest (if perhaps not the best in terms of security). Using this method you could replace your series of if statements on deck[2] by this code

Python:
func = globals()["d"+str(deck[2])]
func()

which will dynamically call one of d2..d11 according to the value of deck[2].

But do you really need 10 different functions to handle the different values of deck[2] ? I have a hunch these functions will look very much alike, with a lot of redundancy between them. Would it not be better to have just one function with the number 2..11 as parameter ?
Thanks! ill check that out. In my case d2() is the function that gets called when the dealer's upcard is a 2 (blackjack game) so I figured it's be much simpler to have 2-11 functions since strategy is determined by the dealer's upcard. I could make it all 1 function but it'd get very big and confusing quickly. If that would make my program considerably faster, then Ill have to figure it out. I attached the WIP below if you wanted to check it out
 

Attachments

Thanks! ill check that out. In my case d2() is the function that gets called when the dealer's upcard is a 2 (blackjack game) so I figured it's be much simpler to have 2-11 functions since strategy is determined by the dealer's upcard. I could make it all 1 function but it'd get very big and confusing quickly. If that would make my program considerably faster, then Ill have to figure it out. I attached the WIP below if you wanted to check it out
Ok, if you expect the functions being significantly different then your approach is the way to go.
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom