问题遇到的现象和发生背景
刚学python写python的一个简单的任务求Π,输入一个类似于1,11,111的数据,然后每一个数据是公式代到最后的分母,不知道为啥报错了
问题相关代码,请勿粘贴截图
# 本程序要求返回算到N_list列表中每一项时的圆周率值,并用列表进行存储,最终输出列表结果
from math import *
N_list = [int(i) for i in input().split(',')]
# 请在此添加实现代码 #
# ********** Begin *********#
result = 0.0000
j = 1
new = []
sign = 1.0000
for i in N_list:
while j <= i:
result = result + (sign / j)
sign = sign*(-1)
j = j + 2
result = result * 4
result = ('%.8f' % result)
new.append(result)
print(new)
运行结果及报错内容
1,11
Traceback (most recent call last):
File "C:/Users/***/PycharmProjects/pythonProject1/main.py", line 15, in <module>
result = result + (sign / j)
TypeError: can only concatenate str (not "float") to str
进程已结束,退出代码为 1
进程已结束,退出代码为 1
我的解答思路和尝试过的方法
我想要达到的结果
这个报错原因是什么