【问题标题】:Python 3 Integration errorPython 3 集成错误
【发布时间】:2018-05-24 04:45:02
【问题描述】:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from scipy.integrate import quad


lo = input("What is your lower bound")
up = input("What is your upper bound")

def f(x):
    return equation

i= int(quad(f(x), lo, up))

while True:
    if i < 0:
        print ("invalid")
    elif i.isalpha:
        print("invalid")
    elif i == "":
        print("invalid")
    else:
        print("invalid")

print(i[0]) 

我遇到了这个错误

TypeError                                 Traceback (most recent call last)
<ipython-input-76-9a1a8ce0b385> in <module>()
     11     return equation
     12 
---> 13 i= int(quad(f(x), lo, up))
     14 
     15 while True:

~/anaconda3/lib/python3.6/site-packages/scipy/integrate/quadpack.py in quad(func, a, b, args, full_output, epsabs, epsrel, limit, points, weight, wvar, wopts, maxp1, limlst)
    321     if (weight is None):
    322         retval = _quad(func, a, b, args, full_output, epsabs, epsrel, limit,
--> 323                        points)
    324     else:
    325         retval = _quad_weight(func, a, b, args, full_output, epsabs, epsrel,

~/anaconda3/lib/python3.6/site-packages/scipy/integrate/quadpack.py in _quad(func, a, b, args, full_output, epsabs, epsrel, limit, points)
    386     if points is None:
    387         if infbounds == 0:
--> 388             return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)
    389         else:
    390             return _quadpack._qagie(func,bound,infbounds,args,full_output,epsabs,epsrel,limit)

**TypeError: must be real number, not str**

【问题讨论】:

  • 什么是equation
  • 下次阅读错误消息 - **TypeError: must be real number, not str** - 并检查变量中的值以找出哪个保留字符串 - 即。 print(variable, type(variable))

标签: python numpy matplotlib integration integral


【解决方案1】:

将输入更改为

lo = float(input("What is your lower bound"))
up = float(input("What is your upper bound"))

默认情况下,input() 返回字符串,但 quad() 需要数字类型的参数。

【讨论】:

    猜你喜欢
    • 2012-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-11
    • 2016-07-18
    • 2022-01-23
    相关资源
    最近更新 更多