Python's my first language, I started learning it before the summer of this year, but never really got into it. Anyway, I'm trying to pick it up.. I wrote a silly little program to convert decimal values to binary. I'd just like suggestions on what could be done in a better, more efficient way. Thanks
Code: Select all
#!/usr/bin/python
def convert(decvalue):
"""This will convert the decimal input value to binary and return it"""
print decvalue,"is the decimal value to convert"
binnum=""
while decvalue != 0:
remain=decvalue%2
decvalue=decvalue/2
if remain == 1:
binnum = "1" + binnum
else:
binnum = "0" + binnum
return binnum
deci=input("Give me the decimal value: ")
print convert(deci)Code: Select all
Give me the decimal value: 21
21 is the decimal value to convert
10101Code: Select all
UnboundLocalError: local variable 'binnum' referenced before assignment
