Python TypeError:必须是str,而不是int

时间:2017-10-05 10:27:41

标签: python math python-3.6

我在Windows 64位上使用Python 3.6.2,我有一个错误: A TypeError ....

   A = 0
   ns = input('Input start:')
   nf = input('Input finish:')
   steps = input('Input steps:')
   for i in range(steps + 1):
       d_n = (nf-ns)/steps
       n = ns + i * d_n
       f_n = n*n
       A = A + f_n * d_n

   next


   print('Area is: ', A)

这是错误......

    Traceback (most recent call last):
      File "C:/Users/UNO/Documents/Python 3.6/Curve_Area2.py", line 5, in 
    <module>
        for i in range(steps + 1):
    TypeError: must be str, not int 

我想要这个结果......

Input start:3
Input finish:5
Input steps:100000
Area is:  32.66700666679996 

我不知道如何解决这个问题......请帮助!!!!

3 个答案:

答案 0 :(得分:2)

编辑:抱歉。使用int(input())来解决问题。输入函数给出str。

ns = str(input('Input start:')

答案 1 :(得分:1)

在这里您要找的是:

A = 0
ns = int(input('Input start:'))
nf = int(input('Input finish:'))
steps = int(input('Input steps:'))
start=[]
finish=[]

for i in range(steps + 1):
    d_n = (nf - ns) / steps

    n = ns + i * d_n
    f_n = n * n
    A = A + f_n * d_n




print('Area is : {} \n Start at {} \n Finish at {} \n steps {}'.format(A,ns,nf,steps))

输入:

Input start:3
Input finish:5
Input steps:1000

输出:

Area is : 32.70066799999998 
 Start at 3 
 Finish at 5 
 steps 1000

答案 2 :(得分:0)

输入函数返回python 3中的字符串。因此,您需要将ns,nf和steps的值转换为整数。
改变这些行

ns = input('Input start:')
nf = input('Input finish:')
steps = input('Input steps:')

ns = int(input('Input start:'))
nf = int(input('Input finish:'))
steps = int(input('Input steps:'))