I need to print the result of 'x/y' but it always returns '0'. When I print 'x' it tells me that correctly, when I print 'y' it tells me that correctly but when I print 'x/y' it says 0.
我需要打印'x / y'的结果,但它总是返回'0'。当我打印'x'时它正确地告诉我,当我打印'y'时它告诉我正确但是当我打印'x / y'时它表示0。
Here is my code:
这是我的代码:
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
Like I said, it prints y
fine when it's supposed to, but in the events that it needs to print x/y
it prints 0
. I have also tried print x/y
and print x//y
but they don't work either.
就像我说的那样,它应该打印得很好,但是在它需要打印x / y的事件中它会打印0.我也尝试过打印x / y并打印x // y但它们也不起作用。
What am I doing wrong?
我究竟做错了什么?
Thanks in advance
提前致谢
4 个解决方案
#1
5
If x and y are both integers, Python always does integer division. So if the result is 0.something, Python will show 0.
如果x和y都是整数,Python总是进行整数除法。因此,如果结果是0.something,Python将显示0。
You need to convert them to floats if you want to do float division:
如果你想做浮动分区,你需要将它们转换为浮点数:
print float(x) / float(y)
or Decimal
objects:
或十进制对象:
from decimal import Decimal
print Decimal(x) / Decimal(y)
#2
5
Do this
做这个
from __future__ import division
Read this
读这个
http://www.python.org/dev/peps/pep-0238/
http://www.python.org/dev/peps/pep-0238/
#3
4
I thought it might be useful to point out the part of that PEP that's most relevant here:
我认为指出那个最相关的PEP部分可能是有用的:
The correct work-around is subtle: casting an argument to float() is wrong if it could be a complex number; adding 0.0 to an argument doesn't preserve the sign of the argument if it was minus zero. The only solution without either downside is multiplying an argument (typically the first) by 1.0. This leaves the value and sign unchanged for float and complex, and turns int and long into a float with the corresponding value.
正确的解决方法是微妙的:如果float()的参数可能是一个复数,则抛出一个参数是错误的;如果参数的符号为零,则向参数添加0.0不会保留参数的符号。没有任何缺点的唯一解决方案是将参数(通常是第一个)乘以1.0。这使float和complex的值和符号保持不变,并将int和long转换为具有相应值的float。
So, you can do:
所以,你可以这样做:
print 1.0 * x / y
#4
1
Try:
尝试:
print float(x)/float(y)
Integer division always truncates the decimal.
整数除法总是截断小数。
Also note that only the numerator actually needs to be a float, but it is better to be explicit about what you are doing.
还要注意,只有分子实际上需要是一个浮点数,但最好明确你正在做什么。
#1
5
If x and y are both integers, Python always does integer division. So if the result is 0.something, Python will show 0.
如果x和y都是整数,Python总是进行整数除法。因此,如果结果是0.something,Python将显示0。
You need to convert them to floats if you want to do float division:
如果你想做浮动分区,你需要将它们转换为浮点数:
print float(x) / float(y)
or Decimal
objects:
或十进制对象:
from decimal import Decimal
print Decimal(x) / Decimal(y)
#2
5
Do this
做这个
from __future__ import division
Read this
读这个
http://www.python.org/dev/peps/pep-0238/
http://www.python.org/dev/peps/pep-0238/
#3
4
I thought it might be useful to point out the part of that PEP that's most relevant here:
我认为指出那个最相关的PEP部分可能是有用的:
The correct work-around is subtle: casting an argument to float() is wrong if it could be a complex number; adding 0.0 to an argument doesn't preserve the sign of the argument if it was minus zero. The only solution without either downside is multiplying an argument (typically the first) by 1.0. This leaves the value and sign unchanged for float and complex, and turns int and long into a float with the corresponding value.
正确的解决方法是微妙的:如果float()的参数可能是一个复数,则抛出一个参数是错误的;如果参数的符号为零,则向参数添加0.0不会保留参数的符号。没有任何缺点的唯一解决方案是将参数(通常是第一个)乘以1.0。这使float和complex的值和符号保持不变,并将int和long转换为具有相应值的float。
So, you can do:
所以,你可以这样做:
print 1.0 * x / y
#4
1
Try:
尝试:
print float(x)/float(y)
Integer division always truncates the decimal.
整数除法总是截断小数。
Also note that only the numerator actually needs to be a float, but it is better to be explicit about what you are doing.
还要注意,只有分子实际上需要是一个浮点数,但最好明确你正在做什么。