Summing a list of floats:TypeError: 'int' object is not callable
Summing a list of floats:TypeError: 'int' object is not callable
所以问题是我不断收到此错误消息:
类型错误:'int' 对象不可调用
range_list = range(1, 97)
for p in range_list:
p_numerator = 4*((-1)**(p+1))
p_denominator = 2*p - 1
function_ofK = float(p_numerator)/float(p_denominator)
total = sum(function_ofK)
print(total)
这是我需要解决的问题:
Problem:
永远记住 sum() 函数接受一个值列表(可迭代)。在你的情况下,你只是传递了一个浮点数。可以从 Python 交互式终端获得有关其使用的一些帮助,如下所示。
Rishikeshs-MacBook-Air:Try hygull$ python3
Python 3.6.7 (v3.6.7:6ec5cf24b7, Oct 20 2018, 03:02:14)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> help(sum)
Help on built-in function sum in module builtins:
sum(iterable, start=0, /)
Return the sum of a 'start' value (default: 0) plus an iterable of numbers
When the iterable is empty, return the start value.
This function is intended specifically for use with numeric values and may
reject non-numeric types.
现在,你可以这样试试。
Using sum()
range_list = range(1, 97)
values = []
for p in range_list:
p_numerator = 4*((-1)**(p+1))
p_denominator = 2*p - 1
function_ofK = float(p_numerator)/float(p_denominator)
values.append(function_ofK)
total = sum(values)
print(total) # 3.131176269454982
Without using sum()
range_list = range(1, 97)
total = 0
for p in range_list:
p_numerator = 4*((-1)**(p+1))
p_denominator = 2*p - 1
function_ofK = float(p_numerator)/float(p_denominator)
total += function_ofK
print(total) # 3.131176269454982
You should try
def get_sum(start, end):
range_list = range(start, end + 1)
total = 0
for p in range_list:
p_numerator = 4*((-1)**(p+1))
p_denominator = 2*p - 1
function_ofK = float(p_numerator) / float(p_denominator)
total += function_ofK
return total
现在您只需调用该函数即可完成工作。
total = get_sum(1, 96)
print(total) # 3.131176269454982
total = get_sum(1, 100)
print(total) # 3.1315929035585537
function_ofK
是一个 float
。 sum
函数需要一个 list
(更具体地说是一个 iterable
)
这就是您可能需要做的。
range_list = range(1, 97)
function_ofK = []
for p in range_list:
p_numerator = 4*((-1)**(p+1))
p_denominator = 2*p - 1
function_ofK.append(p_numerator/p_denominator)
# the total is out of for loop
total = sum(function_ofK)
print(total) # 3.131176269454982
所以问题是我不断收到此错误消息:
类型错误:'int' 对象不可调用
range_list = range(1, 97)
for p in range_list:
p_numerator = 4*((-1)**(p+1))
p_denominator = 2*p - 1
function_ofK = float(p_numerator)/float(p_denominator)
total = sum(function_ofK)
print(total)
这是我需要解决的问题:
Problem:
永远记住 sum() 函数接受一个值列表(可迭代)。在你的情况下,你只是传递了一个浮点数。可以从 Python 交互式终端获得有关其使用的一些帮助,如下所示。
Rishikeshs-MacBook-Air:Try hygull$ python3
Python 3.6.7 (v3.6.7:6ec5cf24b7, Oct 20 2018, 03:02:14)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> help(sum)
Help on built-in function sum in module builtins:
sum(iterable, start=0, /)
Return the sum of a 'start' value (default: 0) plus an iterable of numbers
When the iterable is empty, return the start value.
This function is intended specifically for use with numeric values and may
reject non-numeric types.
现在,你可以这样试试。
Using sum()
range_list = range(1, 97)
values = []
for p in range_list:
p_numerator = 4*((-1)**(p+1))
p_denominator = 2*p - 1
function_ofK = float(p_numerator)/float(p_denominator)
values.append(function_ofK)
total = sum(values)
print(total) # 3.131176269454982
Without using sum()
range_list = range(1, 97)
total = 0
for p in range_list:
p_numerator = 4*((-1)**(p+1))
p_denominator = 2*p - 1
function_ofK = float(p_numerator)/float(p_denominator)
total += function_ofK
print(total) # 3.131176269454982
You should try
def get_sum(start, end):
range_list = range(start, end + 1)
total = 0
for p in range_list:
p_numerator = 4*((-1)**(p+1))
p_denominator = 2*p - 1
function_ofK = float(p_numerator) / float(p_denominator)
total += function_ofK
return total
现在您只需调用该函数即可完成工作。
total = get_sum(1, 96)
print(total) # 3.131176269454982
total = get_sum(1, 100)
print(total) # 3.1315929035585537
function_ofK
是一个 float
。 sum
函数需要一个 list
(更具体地说是一个 iterable
)
这就是您可能需要做的。
range_list = range(1, 97)
function_ofK = []
for p in range_list:
p_numerator = 4*((-1)**(p+1))
p_denominator = 2*p - 1
function_ofK.append(p_numerator/p_denominator)
# the total is out of for loop
total = sum(function_ofK)
print(total) # 3.131176269454982