>>> 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
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to