RECURSION HELP

A forum for general discussion of the Python programming language.

RECURSION HELP

Postby mo711 » Thu Sep 19, 2013 1:44 pm

Hello! i need help, what i need to do is Extract digits using a base, basically i need to write a recursive function dec2base(n, b) that returns the list of base b digits in the positive integer n and the output has to be in this format:

dec2base(120, 10) => [1,2,0] (1*10**2 + 2*10**1 + 0*10**0)

basically, this is what ive written:

Code: Select all
def dec2base(n,b):
    add = n%b
    if n <= 1:
        return str(n)
    else:
        return str(convert(n//b,b)) + str(add)

but i am stuck and dont know what to do. any help???
Last edited by stranac on Thu Sep 19, 2013 1:49 pm, edited 1 time in total.
Reason: Added code tags, locked post
mo711
 
Posts: 1
Joined: Thu Sep 19, 2013 1:37 pm

Re: RECURSION HELP

Postby stranac » Thu Sep 19, 2013 1:52 pm

Your function is not called convert, it's dec2base...
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm


Return to General Discussions

Who is online

Users browsing this forum: Google [Bot] and 2 guests