Nov-10-2017, 10:34 AM
Hello, I got this assignment of incrementing a binary number (which displays as a string) without using base converting.
i wrote the code which i think is pretty good, but i got stuck with an error message when used this line of code:
return ''.join(lst_binary.insert(0,'1'))
that happens when the prog gets a string like '111', and needs to add another digit at the end of number.
the error i got was
Error:"TypeError: can only join an iterable"
while was debugging with python-tutorthis is the whole code:
1 2 3 4 5 6 7 8 9 |
def inc(binary): lst_binary = list (binary) for bit in range ( len (binary) - 1 , - 1 , - 1 ): if lst_binary[bit] = = '0' : lst_binary[bit] = '1' return ''.join(lst_binary) else : lst_binary[bit] = '0' return ' '.join(lst_binary.insert(0,' 1 ')) |