以下是修改后的代码:

def my_count(num_list, target):
    count = 0
    for num in num_list:
        if num == target:
            count += 1
    return count

test_list = [1, 2, 3, 4, 2, 2, 3]
test_target = 2
result = my_count(test_list, test_target)
print(result)

修改说明:

  • 在函数定义时添加了参数 'num_list' 和 'target',以便在函数内部使用。
  • 在函数内部使用了 'num_list' 和 'target',而不是 'list' 和 'target'。
  • 修改了 'return count' 的缩进,使其成为函数 'my_count' 的返回值。
Python 代码优化:解决报错并改进计数函数

原文地址: http://www.cveoy.top/t/topic/mJE8 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录