方法1:循环运算

num = int(input())
count = 0
while num > 1:
    if num % 2 == 1:
        count += 1
    num = num // 2
if num % 2 == 1:
    count += 1
print(count)

方法2:位移运算:

num = int(input()) 
counts = 0 
while num !=0: 
    if num % 2 == 1: 
        counts = counts+1 
    num = num >> 1 
print(counts)