Home > front end >  Getting repetitions of one in the base 2 representation of a number python
Getting repetitions of one in the base 2 representation of a number python

Time:07-05

I want to enter a number in the input and get the number of occurrences of one in its binary basis

print(str({int(input()):'b'}).count('1'))

The code I wrote is like this, but for example:

input:3
output:0

That is wrong

CodePudding user response:

Proceed as follows:

   print("{:b}".format(int(input())).count('1'))

input:3
output:2
  • Related