- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建我自己的“分数类”。几乎所有代码都可以正常工作,但在 /
, >
, <
,这些都不起作用。我不知道我的代码有什么问题。
def gcd(m, n):
while m%n != 0:
m, n = n, m%n
return n
class Fraction:
'''Fractional class'''
def __init__(self, num, denom):
self.num = num
self.denom = denom
def __str__(self):
return str(self.num)+'/'+str(self.denom)
def __add__(self, other):
new_num = self.num * other.denom + other.num * self.denom
new_denom = self.denom * other.denom
common = gcd(new_num, new_denom)
return Fraction(new_num//common, new_denom//common)
def __sub__(self, other):
new_num = self.num * other.denom - other.num * self.denom
new_denom = self.denom * other.denom
common = gcd(new_num, new_denom)
return Fraction(new_num//common, new_denom//common)
def __mul__(self, other):
new_num = self.num * other.num
new_denom = self.denom * other.denom
common = gcd(new_num, new_denom)
return Fraction(new_num//common, new_denom//common)
def __div__(self, other):
new_num = self.num * other.denom
new_denom = self.denom * other.num
common = gcd(new_num, new_denom)
return Fraction(new_num//common, new_denom//common)
def __equal__(self, other):
return (self.num * other.denom) == (other.num * self.denom)
def __big__(self, other):
return str(self.num * other.denom) > str(other.num * self.denom)
def __small__(self, other):
return self.num * other.denom < other.num * self.denom
if __name__ == "__main__":
f1 = Fraction(1,4)
f2 = Fraction(1,2)
print(f1+f2)
print(f1 - f2)
print(f1 * f2)
print(f1 / f2) #not working
print(f1 == f2)
print(f1 > f2) #not working
print(f1 < f2) #not working
我得到以下输出:
3/4
-1/4
1/8
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-50-50cad0951bd1> in <module>()
57 print(f1 - f2)
58 print(f1 * f2)
---> 59 print(f1 / f2) #not working
60
61 print(f1 == f2)
TypeError: unsupported operand type(s) for /: 'Fraction' and 'Fraction'
我定义__div__
吗?正确吗?
最佳答案
Python 3.x 使用 __truediv__
和 __floordiv__
。 __div__
用于 python 2.x。
要进行比较,您需要定义 __lt__
、__gt__
、__ge__
和 __le__
。
关于python - 我自己制作分数课,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49340174/
我正在为数据结构类(class)做期末练习,我有几个问题希望得到帮助: void BST::traverse(Vertex *V) // post order traversal recursive
我正在 udacity.com 上进行 Android 开发培训,随后进行了 sunny 应用程序的实现。我正在使用 android studio 最新版本进行实现。 我正要到达我应该获得模拟 Lis
~~~更新:已解决!谢谢大家!~~~ 我正在研究 Blue Pelican Java 书中的一个项目,第 16 课项目 Gas Mileage。它要求创建两个类,一个是 Automobile,它包含我
我已经阅读了很多有关依赖注入(inject)、控制反转和 IoC 容器的文章。我还主要使用动态语言编程(工作中使用 PHP,在家使用 Python)。以下是我找到的东西,但是当我将它们拼凑在一起时,这
我有以下类层次结构: interface Repository // This class contains some common stuff for LocalRepository and Rem
我正在观看 Erik Meijer 的 Functional Programming Fundamentals 系列讲座(附有 Graham Hutton 的幻灯片)。 在 lecture 8 (on
一个文本文件包含有关垒球队的信息。每行数据排列如下: 4 Jessie Joybat 5 2 1 1 第一项是玩家的编号,方便地在 0-18 范围内。第二项是玩家的名字,第三项是玩家的姓氏。每个名字都
我正在学习 Codility 计数课 ( https://codility.com/media/train/2-CountingElements.pdf ),我需要帮助来了解最快的解决方案。 我想知道
我是编码新手,即将完成“使用 Swift 进行 App 开发入门”iBook。我目前正在学习第 19 课,枚举和开关,在相关 Playground 的第 8 页,它显示了以下代码: enum Lunc
我真的尝试过研究这个问题,但我现在离它太近了,我担心如果不寻求帮助我就找不到解决方案。我正在学习 RubyMonk,其中一个练习让我完全不知所措。 class Hero def initializ
在观看 Lecture 10 iTunes 视频的同时尝试跟进并编写 Smashtag 项目。 当我将下载的 Twitter 包添加到我的 Smashtag 项目时,当我在 TweetTableVie
public int solution(int[] A) { int lengthOfArray = A.length; int tempArray[] = new int[lengt
所以我收到此错误消息,指出无法解析fragment_ main xml 中的符号它出现在这行代码上 tools:context=".MainActivity$ForecastFragment">其中的
使用 NodeSchool.io 学习 Node.js,我对以下两个代码段之间的差异感到困惑。这种差异可能对于 Node.js 或一般的 JS 来说是根本性的,所以我希望专家能够向我澄清这一点。 第
这是我想为每个行为类似于切片的类型实现的特征(针对问题进行了简化): trait SliceLike { type Item; /// Computes and returns (ow
这节课应该非常简单。他们拼出了答案,但我自己和论坛中的 10 多个人无法让本类(class)发挥作用。 我们是否错误地实现了代码?教训是否具有误导性?答案检查器是否已损坏? http://www.co
我将在 udacity.com 上接受 Android 开发人员培训,然后实现 Sunshine 应用程序。我正在使用 Android Studio,最新版本默认安装。 我正处于我应该拥有一个带有模拟
这个问题已经有答案了: Why is "None" printed after my function's output? (7 个回答) 已关闭 6 年前。 我已经在互联网上搜索并尝试了代码的变体,
我无法在我的 Rails 应用程序中使用 Ajax。 借助 Ajax 的魔力:当我单击“关注”按钮时,它应该更新用户在个人资料页面中看到的关注者数量,而无需刷新页面。但是,在我的示例应用程序中,这并没
我正在关注关于 Redux 的 egghead.io 教程。我在第 17 课上遇到了 Dan Abramov 没有的错误。代码如下。 我得到的错误是 “类型错误:无法读取未定义的属性‘map’ 根据我
我是一名优秀的程序员,十分优秀!