[Tutor] Can't figure out syntax error

as.20.schellenberg@spamgourmet.com as.20.schellenberg at spamgourmet.com
Fri Jun 10 02:16:59 CEST 2005


Hi there,

I'm in the process of learning Python, and need some help deciphering 
the reason why the following code doesn't work:

import sys, string

def dec2bin(decNum):
	# validate the input as a postive integer number
	for char in decNum:
		if str(char).isdigit() == False:
			print "Not a postive integer number given when calling the dec2bin 
function."
			sys.exit()
	
	bin = ""					# initialize the new binary result (as a string)
	num = int(decNum)
	
	if num == 0:				# take care of the zero case
		bin = "0"
	
	while int(num) != 0:				# the rest of the cases
		nextBin = int(num) % 2		# check if this column should be a 0 or 1
		bin = str(nextBin) + bin		# add the result to the front of the result 
string
		int(num) = int(num) / 2		# this is integer division, so we truncate 
the decimal part
	
	return bin						# return the binary result

# testing
x = "13"
print dec2bin(x)


I get the following error:

> File "convert.py", line 42
>     int(num) = int(num) / 2             # this is integer division, so 
> we truncate the decimal part
> SyntaxError: can't assign to function call


Any help anyone can provide would be greatly appreciated.
Dan



More information about the Tutor mailing list