TypeError:需要浮动 [英] TypeError: a float is required
本文介绍了TypeError:需要浮动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
无法发布图片,因此: a [i] = {( - 1)^(i + 1)* sin(x)* ln(x)} / {i ^ 2 * (i + 1)!}
Can't post image, so: a[i]={(-1)^(i+1)*sin(x)*ln(x)}/{i^2*(i+1)!}
任务:
需要查找a1,a2,...,
n是自然的,并且是给定的。
The task:
Need to find a1,a2,...,an.
n is natural and it's given.
这就是我试图这样做的方式:
That's the way I tried to do this:
import math
a=[]
k=0
p=0
def factorial(n):
f=1
for i in range(1,n+1):
f=f*i
return f
def narys(n):
x=input('input x: ') #x isn't given by task rules, so i think that is error else.
float(x)
k=(math.pow(-1,n+1)*math.sin(x)*math.log10(n*x))/(n*n*factorial(n+1))
a.append=k
n=int(input('input n: '))
narys(n)
for i in a:
print(a[p])
p=p+1
推荐答案
好像你正在使用Python 3.x版本。 输入
调用的结果是从键盘获取的字符串,您将其传递给 math.sin(.. 。)
函数。 float(x)
将 x
转换为 float
在任何地方存储转换后的值,所以改变:
Seems like you're using Python 3.x version. The result of an input
call is a string taken from keyboard, which you pass to a math.sin(...)
function. float(x)
converts x
to float
but does not store the converted value anywhere, so change:
float(x)
到:
to:
x = float(x)
以获得正确的代码行为。
to get right behaviour of your code.
这篇关于TypeError:需要浮动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文