Trying to write code for converting decimal number into binary but it is showing output ending with "none" | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 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)

19th Sep 2022, 2:50 PM
vishwajeet pradip ghuge
vishwajeet pradip ghuge - avatar
2 Réponses
19th Sep 2022, 2:55 PM
Jayakrishna 🇮🇳
+ 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
19th Sep 2022, 3:24 PM
Solo
Solo - avatar