Python逆转换? (分数)

时间:2018-04-16 15:19:22

标签: python python-3.x python-3.6

在Python 3.6.4中编码,系统基本上打印“4/1”,“4/3”,“4/5”,“4/7”等。结果是正确的,因为它确实打印了这些值,但它使它们看起来是可能的最大数量,除了第一个。

4
6004799503160661/4503599627370496
3602879701896397/4503599627370496
2573485501354569/4503599627370496
2001599834386887/4503599627370496

我输入的代码就像这样

from fractions import Fraction
q = int(input("Choose a number: "))
a = 4
count = q
w = 1
while count > 0:
    print(str(Fraction(a/w)))
    count = count - 1
    w = w + 2

有谁知道如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

我认为你应该这样做:~,而不是print(str(Fraction(a, w)))。这里发生的是你获得print(str(Fraction(a/w)))的价值,然后试图使它成为一个分数,这会产生你得到的奇怪数字。

比较

a/w