[Tutor] need help with 3.1's fraction module
bob gailer
bgailer at gmail.com
Wed Jan 6 20:59:03 CET 2010
Richard D. Moores wrote:
>>>> 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?
>
Fraction(str(random())).limit_denominator(100)
--
Bob Gailer
Chapel Hill NC
919-636-4239
More information about the Tutor
mailing list