python的工作方式包括_关于Python中元组工作方式的几个问题

所以,我在麻省理工学院开放式课程《计算机科学与编程导论》中做了一个问题:Problem #2

Implement the compute_deriv function. This function computes the derivative

of a polynomial function. It takes in a tuple of numbers poly and returns

the derivative, which is also a polynomial represented by a tuple.

def compute_deriv(poly):

"""

Computes and returns the derivative of a polynomial function. If the

derivative is 0, returns (0.0,).

Example:

>>> poly = (-13.39, 0.0, 17.5, 3.0, 1.0) # x4 + 3.0x3 + 17.5x2 - 13.39

>>> print compute_deriv(poly) # 4.0x3 + 9.0x2 + 35.0x

(0.0, 35.0, 9.0, 4.0)

poly: tuple of numbers, length > 0

returns: tuple of numbers

"""

# TO DO ...

这是我的程序(有效):

^{pr2}$

首先,我想让程序要求我输入多项式,而不是把它写在里面:...

polyx=tuple(raw_input("Enter your polynomial tuple here:"))

print compute_deriv(polyx)

但那没用:Enter your tuple here:-13.39, 0.0, 17.5, 3.0, 1.0

('1', '33', '...', '33', '99999', ',,,,,,', ' ', '00000000', '...',

'00000000', ',,,,,,', ' ', '1', '77777777777777', '...',

'5555555555555555', ',,,,,,', ' ', '33', '...', '00000000', ',,,,,,',

' ', '1', '...', '00000000')

为什么?

另一个问题是第二个元组(-8x^5+2.5x^3+4x^2+7x+1.3)-当其成员分别为(1.3、7.0、4.0、2.5、0.0、-8.0)时,它会按预期返回-(7.0、8.0、7.5、0.0、-40.0),但如果第一个元组是0.0(如-8x^5+2.5x^3+4x^2+7x),则会发生变化-(7.0、8.0、7.5、-40.0)。第二个0.0被省略了,这是一个问题,因为它意味着-40.0的幂是3,当它是4时。再说一遍,为什么?在

感谢您抽出时间!在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值