+ 1
Trying to write code for converting decimal number into binary but it is showing output ending with "none"
like if input is "4"....it is generating output as "100None" keeping last block of code as it is how can we solve this problem? (i.e. if __name__ block) def D_to_B(n) binaryNum = [0] * n; i = 0; while (n > 0): binaryNum[i] = n % 2; n = int(n / 2); i += 1; for j in range(i - 1, -1,-1): print(binaryNum[j], end = ""); if __name__ == '__main__': n = int(input().strip()) result = D_to_B(n) print (result)
2 Antworten
+ 2
First, add ":" in the function definition.
Your function prints, so you essentially get a print(print()) ☺️
Debug:
out = ''
for j in range(i - 1, -1,-1):
out += str(binaryNum[j])
return out