vendredi 22 octobre 2021

How do you access a function name not what the function returns in python?

My question pertains specifically to the random module, in my code when i save a random number to a variable or within a list my function becomes a permanent number.

def d4(num=None):
    if num is None:
        num = random.randint(1, 4)
        return num


def d6(num=None):
    if num is None:
        num = random.randint(1, 6)
        return num 

(self explanatory for the rest of dice)

diceDict = {'d4': d4(), 'd6': d6(), 'd8': d8(), 'd10': d10(), 'd12': d12(), 'd20': d20()}
player1 = [d4(), d4()]
player2 = [d4(), d4()]

def getKey(die):
    for key, value in diceDict.items():
        if die == value:
            return key

print(f'P1 dice are: {getKey(player1[0])} and {getKey(player1[1])}')
        print(f'P2 dice are: {getKey(player2[0])} and {getKey(player2[1])}')

so what this is supposed to do is tell them what dice they have in their hand but what it does is... say the d8() in diceDict rolls a 2, then every time a dice rolls a 2 it says they have a d8. please help!




Aucun commentaire:

Enregistrer un commentaire