Tai*_*mur 3 python printing random ironpython division
我需要打印'x/y'的结果,但它总是返回'0'.当我打印'x'时,它正确地告诉我,当我打印'y'时它正确告诉我但是当我打印'x/y'时它表示0.
这是我的代码:
import random
y = 0
x = 0
p = 1
while True:    
    i = [random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100)]
    if len(set(i)) < len(i):
        print "Match!"
        x += 1
        y += 1
        print(x/y)  
    else:
        print "No Match!"
        y += 1
        print y
就像我说的那样,它应该打印得y很好,但是在需要打印x/y它的事件中打印0.我也尝试过打印x/y和打印,x//y但它们也不起作用.
我究竟做错了什么?
提前致谢
如果x和y都是整数,Python总是进行整数除法.因此,如果结果是0.something,Python将显示0.
如果你想做浮动分区,你需要将它们转换为浮点数:
print float(x) / float(y)
或Decimal对象:
from decimal import Decimal
print Decimal(x) / Decimal(y)
做这个
from __future__ import division
读这个
http://www.python.org/dev/peps/pep-0238/