I'm making a small program to convert binary numbers to hexadecimal. To go about this, I first split the given data into 4 bit chunks as you can see.
if len(n)%4 != 0:
n = "0"+n
Here is the code we're discussing:
def binary_to_hex(n): if len(n)%4 != 0: n = "0"+n binary_to_hex(n) # no `return' return n
binary_to_hex(n) calls the function and ignores the result. Since the call has no observable side effects (i.e. it doesn't do anything that the caller can observe), it is effectively a no-op, and the code is equivalent to:
def binary_to_hex(n): if len(n)%4 != 0: n = "0"+n return n
This explains the behaviour you're seeing.
[H]ow one would go about without using the first return statement[?]
Try using a loop or the string repetition operator (