[英]Python-Sum Of Digits Raised to Powers
def sum_powers(x1, n1, x2, n2):
""" Computes x1^n1+x2^n2
x1,n1,x2,n2 -- numbers
Use only call expressions,
Not infix operators(+,-,...) """
return __________________________
Does anyone know what I have to do here? 有人知道我在这里做什么吗? I know which sum I am supposed to compute, but I do not really understand my teacher's instructions. 我知道我应该计算哪个总和,但是我并不真正理解老师的指示。 Can someone help? 有人可以帮忙吗? Thanks a lot. 非常感谢。
So to the best of my guess , the instructor wants you to use the dunder method format . 因此,据我所知,讲师希望您使用dunder方法格式。 like : __add__
, __sub__
etc . 例如: __add__
, __sub__
等。 in your code , so that you understand how python implements those operators , or which magic methods represent those operators. 在您的代码中,以便您了解python如何实现这些运算符,或哪些魔术方法表示这些运算符。
Operator Method
+ object.__add__(self, other)
- object.__sub__(self, other)
* object.__mul__(self, other)
// object.__floordiv__(self, other)
/ object.__truediv__(self, other)
% object.__mod__(self, other)
** object.__pow__(self, other[, modulo])
<< object.__lshift__(self, other)
>> object.__rshift__(self, other)
& object.__and__(self, other)
^ object.__xor__(self, other)
| object.__or__(self, other)
example ( can be done on other ways): 示例(可以通过其他方式完成):
def sum_powers(x1, n1, x2, n2):
""" Computes x1^n1+x2^n2
x1,n1,x2,n2 -- numbers
Use only call expressions,
Not infix operators(+,-,...) """
p1 = x1.__pow__(n1)
p2 = x2.__pow__(n2)
return p1.__add__(p2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.