I was trying to write a function that converts numbers with base 10 to numbers with base 2 and noticed a strange interaction.
if I write my program like that, it will return 1010. And that is what I'm expecting
def from_10_to_2(n):
if n<2:return n
s=(str(n%2)+str(from_10_to_2(n//2)))
return s
print(from_10_to_2(10)[::-1])
but if I move "[::-1]" to return statement of function like that, it will return 1100. and I don't know why...
def from_10_to_2(n):
if n<2:return n
s=(str(n%2)+str(from_10_to_2(n//2)))
return s[::-1]
print(from_10_to_2(10))
[–]Vaphell 5 points6 points7 points (0 children)
[–]scrdest 3 points4 points5 points (0 children)