Really new to python and maybe I am just missing something that is quite easy. I am trying out a simple recursive function that you would assign to a freshman CS class and I am trying to do in in Python in an attempt to learn the language. I can't seem to get this correct, it prints the correct digits but I am trying to return a string but I keep having issues. I have tried declaring result inside the function, but then it gets cleared out on every pass. I have tried passing in a variable along with the integer, but it too clears itself out with each iteration. Any point in the right direction would be great. Thanks.

def binaryRepresentation(m) :
	if m == 0:
		return
	else:
		if m > 0:
			result = (m % 2)
			binaryRepresentation(m/2)
#			print result
			return result
numberInputted = int(raw_input("Enter a number to be converted: "))
print binaryRepresentation(numberInputted)

It looks like you've got the idea of using recursion to solve the problem. What I see as a problem (besides not using code blocks) is that you're not doing anything with the results of your recursion. You call your function, but you don't do anything with the results. You're so close that I can't really push you in the right direction without giving you the whole solution. So here is my working code.

def binresult(m):
  # If you get a zero then return nothing.  If you return zero then you
  # get a leading zero that we dont want.
  if m == 0:
    return ''

  # Here is the recursion.  We are going to run this same function on
  # another number and put the result in front of our own answer.
  return binresult(m/2) + str(m%2)

binresult(input("Please enter an integer: "))

Thanks for the help. I guess I will go back to reading the basics of Python once again. I figured it was syntax. I knew in my example I wasn't doing anything with the result because I couldn't get it to do what I wanted so I commented it out temporarily. Two quick questions, you mentioned code blocks? I was under the impression in Python, that the "code blocks" were simply created by the indentation? Maybe I read the wrong blog. The other quick question is what is the difference between input and raw_input? After looking at your code I would think that simply input may infer the data type??? Not sure though. Like I said I guess I will have to pickup a good Python book. Just figured I would learn a new language that was completely different from what I have been using lately. Thanks for the help.

What I mean by code blocks are specific to this board. When you post code on the board you should enclose the code in code tags so that it is more readable. You are correct that in the Python language code is grouped together by indentation only.

The difference between input and raw_input is that input accepts an integer and raw_input accepts a string. The general consensus on this board seems to be that you should almost always use raw_input and then use other code to validate the response. A different thread talked about security vulnerabilities that could open up when you assume that the user is going to enter an integer. I used input for this example because I was just demonstrating and I didn't want to muddy the water by validating the input, converting to an integer, etc.

What I mean by code blocks are specific to this board. When you post code on the board you should enclose the code in code tags so that it is more readable. You are correct that in the Python language code is grouped together by indentation only.

The difference between input and raw_input is that input accepts an integer and raw_input accepts a string. The general consensus on this board seems to be that you should almost always use raw_input and then use other code to validate the response. A different thread talked about security vulnerabilities that could open up when you assume that the user is going to enter an integer. I used input for this example because I was just demonstrating and I didn't want to muddy the water by validating the input, converting to an integer, etc.

I agree! Most IDE editors made for Python, like IDLE or DrPython, will help you with the indented blocks. Pretty much the standard is to indent 4 spaces. Don't use tabs since they change with the editor settings.

The good news is that the latest Python version 3.0 (aka Python30) has done away with the input()/raw_input() confusion. From now on it's only input() and that one returns a string.

This article has been dead for over six months. Start a new discussion instead.