How do I convert a decimal integer to a binary number using a recursive function? I am really stuck on this one. any help?
decimal to python using recursion
Collapse
X
-
Originally posted by wagn31How do I convert a decimal integer to a binary number using a recursive function? I am really stuck on this one. any help? -
Originally posted by wagn31yeah i know how to do that but i am new to python and am unsure how the coding goesCode:ans = '' while num != 0: num, rem = divmod(...........
The key operator is '%' (modulo).
Using recursion:
Code:....num, rem = divmod(................. return function_name(............
Comment
-
[CODE=python]def print_binary1(d ecimal_string):
bStr = ''
while decimal_string > 0:
bStr = str(decimal_str ing % 2)
decimal_string = decimal_string >> 1
print bStr
return bStr
if decimal_string == 0:
print bStr
return bStr
print_binary1(d ecimal_string )
[/CODE]
this is what i have and i know i am not even close....pleas help!!!!Comment
-
decimal to python using recursion
I need to write a code that take a decimal string and turns it into a binary number and i am new at python and have no clue how to do this:
[CODE=python]
def print_binary1(d ecimal_string):
bStr = '0'
while decimal_string > 0:
bStr = str(decimal_str ing % 2)
decimal_string = decimal_string >> 1
print bStr
return bStr
if decimal_string == 0:
print bStr
return bStr
print_binary1( )
[/CODE]
this is what I have, i know I am wrong but cant figure it out....someone please help!!!!!Comment
-
Originally posted by wagn31I need to write a code that take a decimal string and turns it into a binary number and i am new at python and have no clue how to do this:
[CODE=python]
def print_binary1(d ecimal_string):
bStr = '0'
while decimal_string > 0:
bStr = str(decimal_str ing % 2)
decimal_string = decimal_string >> 1
print bStr
return bStr
if decimal_string == 0:
print bStr
return bStr
print_binary1( )
[/CODE]
this is what I have, i know I am wrong but cant figure it out....someone please help!!!!!Comment
-
Originally posted by wagn31def print_binary1(d ecimal_string):
bStr = ''
while decimal_string > 0:
bStr = str(decimal_str ing % 2)
decimal_string = decimal_string >> 1
print bStr
return bStr
if decimal_string == 0:
print bStr
return bStr
print_binary1(d ecimal_string )
this is what i have and i know i am not even close....pleas help!!!!
if decimal_string == 0:
return ''
............... ............... ........... # a line from your code
............... ............... ........... # a line from your code
return print_binary1(d ecimal_string)+ bStr[/code]I do not like your choice of name for decimal_string. It implies that the variable is a string, but it must be an integer for this code to work.
You need to place code tags around your code when you post. This is an open code tag: [code=Python]
This is a close code tag: [/ c o d e ]
I put spaces in the close tag so it would display.Comment
-
Comment
-
Originally posted by bvdetI just realized that this is a double posted thread. Please follow site guidelines: double post
wagn31:
[ CODE ] tags are another stipulation of the Posting Guidelines. Please read them and follow them.Comment
Comment