[Tutor] need help with 3.1's fraction module

Richard D. Moores rdmoores at gmail.com
Wed Jan 6 20:29:25 CET 2010


>>> from fractions import Fraction
>>> Fraction('.83452345').limit_denominator(100)
Fraction(81, 97)

I'd like to know how to do that in a script, where numbers like
.83452345 are the output of random.random().
>>> Fraction(random()).limit_denominator(100)

Doesn't work, nor did I really expect it to:
>>> Fraction(random()).limit_denominator(100)
Traceback (most recent call last):
  File "C:\Program Files (x86)\Wing IDE
3.2\src\debug\tserver\_sandbox.py", line 1, in <module>
    # Used internally for debug sandbox under external interpreter
  File "C:\Python31\Lib\fractions.py", line 100, in __new__
    raise TypeError("argument should be a string "
builtins.TypeError: argument should be a string or a Rational instance
>>>

But what does work?

Thanks,

Dick Moores


More information about the Tutor mailing list