Using Fractions in Python

那年仲夏 提交于 2019-12-08 01:31:24

You don't need your own class to multiply two fractions:

>>> from fractions import Fraction as F
>>> F("1/2")
Fraction(1, 2)
>>> F("3/4")
Fraction(3, 4)
>>> F("1/2") * F("3/4")
Fraction(3, 8)
>>> F("1/2") + F("3/4")
Fraction(5, 4)
>>> F(5, 8) + F(4, 7)
Fraction(67, 56)

As for the error you mentioned, it's unlikely as you don't have a name "Fractions" anywhere in your code and you didn't post a traceback. You're most likely running some old version of your code.

There is one more space at the line 26:

         except NameError:

It should be

        except NameError:

You should also check and fix the indentation. Otherwise, there is no body -- nothing is called.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!