Does anybody have an idea why this function would not generate an output?
import math
def converter (value):
list1 = []
list1.reverse()
while value >= 0:
Remainder = value%2
list1.append(Remainder)
value /= 2
math.floor(value)
list1.reverse()
return list1
converter(17)
The model would not generate an output. After debugging I found the source of the problem value /= 2 changing this to something like value -=2 seem to enable the code to run, however, it is not the output I want.
>Solution :
Multiple problems with your code. You want to change your condition in the while loop as it runs forever. Also, you need to change the the new assignment of the variable value:
import math
def converter (value):
list1 = []
while value > 0:
remainder = value % 2
list1.append(remainder)
value = math.floor(value / 2)
list1.reverse()
return list1
converter(17)
Output:
[1, 0, 0, 0, 1]
Edit: I would also remove math.floor() by an integer division:
def converter (value):
list1 = []
while value > 0:
remainder = value % 2
list1.append(remainder)
value = value // 2
list1.reverse()
return list1
converter(18)