在ipython notebook中,“TypeError:不是在字符串格式化期间转换的所有参数”

时间:2015-05-16 23:14:43

标签: python ipython sympy

嗨,我正在学习线性代数与python和Edx课程。 (http://nbviewer.ipython.org/github/ULAFF/notebooks/tree/may-14-2014/)。

使用第一个框开启“02.4.2.10使用矩阵向量乘法练习”,代码为:

import generate_problems as gp
print("What is the result of the matrix vector product below?")

p = gp.Problem()

p.new_problem()
generate_problems is a module that the professor at Edx created. However, I got an error importing sympy.

当我尝试执行上面的代码时,我收到以下错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-79d56e0988cb> in <module>()
      2 print("What is the result of the matrix vector product below?")
      3 
----> 4 p = gp.Problem()
      5 
      6 p.new_problem()

/Users/user/Desktop/Course/Python/ipython/notebooks-master/generate_problems.pyc in __init__(self)
     14                 tag = ''
     15 
---> 16                 A = self.random_integer_matrix( m, n )
     17                 x = self.random_integer_matrix( n, 1 )
     18 

/Users/user/Desktop/Course/Python/ipython/notebooks-master/generate_problems.pyc in random_integer_matrix(self, m, n)
     26 
     27         def random_integer_matrix( self, m, n ):
---> 28                 A = zeros( (m, n) )
     29 
     30                 for i in range( m ):

/Users/user/anaconda/lib/python2.7/site-packages/sympy/matrices/dense.pyc in zeros(r, c, cls)
   1227     if cls is None:
   1228         from .dense import Matrix as cls
-> 1229     return cls.zeros(r, c)
   1230 
   1231 

/Users/user/anaconda/lib/python2.7/site-packages/sympy/matrices/dense.pyc in zeros(cls, r, c)
    511         """Return an r x c matrix of zeros, square if c is omitted."""
    512         c = r if c is None else c
--> 513         r = as_int(r)
    514         c = as_int(c)
    515         return cls._new(r, c, [cls._sympify(0)]*r*c)

/Users/user/anaconda/lib/python2.7/site-packages/sympy/core/compatibility.pyc in as_int(n)
    387             raise TypeError
    388     except TypeError:
--> 389         raise ValueError('%s is not an integer' % n)
    390     return result
    391 

TypeError: not all arguments converted during string formatting

对于compatibility.py,我似乎太少了%s?我在Mac OSX Yosemite上使用Anaconda。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

看起来课程代码不支持最新版本的SymPy,它已经进行了API更改(zeros()现在的工作方式与zeros(r, c)类似,而不是zeros((r, c)),因为它在{ {1}})。您可以通过编辑generate_problems.py中的代码来修复它。