def print_binary1(decimal_string):
bStr = ''
while decimal_string > 0:
bStr = str(decimal_string % 2)
decimal_string = decimal_string >> 1
print bStr
return bStr
if decimal_string == 0:
print bStr
return bStr
print_binary1(decimal_string )
this is what i have and i know i am not even close....pleas help!!!!
You are not that far off. Since you are using recursion, you will not need a while loop nor should you assign 'bStr' to ''. You WILL need a way to end the recursion.
- if decimal_string == 0: return ''
You can build the result on a return statement:
- return print_binary1(decimal_string)+bStr
This should work for you:
- def print_binary1(decimal_string):
-
if decimal_string == 0:
-
return ''
-
......................................... # a line from your code
-
......................................... # a line from your code
-
return print_binary1(decimal_string)+bStr
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.