Search code examples
pythonrecursiontail-recursion

Why is this recursive script returning 'None'?


The code below is intended to use the number of aces and the number of points you'd have if the aces were low in blackjack and make the list of numbers you could pick to have by changing if the ace is high/low.

acenum() is the recursive module, numoface is the number of aces, and cardtotallocal is the local variable for the card total (ace low)

def acenum(numoface, cardtotallocal):
    if numoface==1:
        return print(f'{cardtotallocal}/{cardtotallocal+10}')
    else:
        return print(f'{acenum(numoface-1, cardtotallocal)}/{cardtotallocal+numoface*10}')

acenum(4, 23)

However the code (above) gives this output (below).

23/33
None/43
None/53
None/63

I'm not sure where the 'None' and the '/n' comes from: I'd appreciate some help! Thanks for reading, I hope you can help.


Solution

  • Print function return can't be used as input for the recursive function. Try this:

    def acenum(numoface, cardtotallocal):
        if numoface==1:
            return cardtotallocal/(cardtotallocal+10)
        else:
            return acenum(numoface-1, cardtotallocal)/(cardtotallocal+numoface*10)
    
    print(acenum(4, 23))
    
    # or
    
    Variable = acenum(4, 23)
    print(Variable)