Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:count_bits()接受0个位置参数,但给出了1个_Python - Fatal编程技术网

Python TypeError:count_bits()接受0个位置参数,但给出了1个

Python TypeError:count_bits()接受0个位置参数,但给出了1个,python,Python,我在Codewars做我的任务,在那里我需要写一个程序来计算5的二进制表示中1的数量。但是,我得到了以下错误: TypeError: count_bits() takes 0 positional arguments but 1 was given 我也试图找到一些答案,但没有一个有效 我的代码: def count_bits(): pass solution1 = 0 binaryfive = format(bin(5)) print(binaryfive)

我在Codewars做我的任务,在那里我需要写一个程序来计算5的二进制表示中1的数量。但是,我得到了以下错误:

TypeError: count_bits() takes 0 positional arguments but 1 was given
我也试图找到一些答案,但没有一个有效

我的代码:

def count_bits():
    pass
    solution1 = 0
    binaryfive = format(bin(5))
    print(binaryfive)
    for i in binaryfive
        if i == str(1):
            solution1 += 1
    return

函数应该接受数字作为参数,并将计数作为其返回值返回。显然,您的测试人员正在尝试使用参数调用它。当然,CodeWars的全部目的是自己完成任务,但令人失望的是,您无法使用非常有用的错误消息来解决此问题。也许你可以在这里学到一些东西,用于下一个任务

def count_bits(number):
    binary = bin(number)
    return binary.count('1')

没有代码的图像。将代码以正确的格式粘贴到此处。为什么函数以
pass
语句开头,为什么函数的返回值为空?