python中的除法。7/9=0?如何阻止这一切?

2024-04-24 01:26:02 发布

您现在位置:Python中文网/ 问答频道 /正文

Possible Duplicate:
how can I force division to be floating point in Python?

如果这个问题已经被问过了,我很抱歉。在

timothy_lewis_three_pointers_attempted = 4
timothy_lewis_three_pointers_made = 2

print 'three pointers attempted: ' + str(timothy_lewis_three_pointers_attempted)
print 'three pointers made: ' + str(timothy_lewis_three_pointers_made)
print 'three point percentage: ' + str(timothy_lewis_three_point_percentage)

我的百分比是0。我怎么才能让它说。5?我知道如果我输入4.0和2.0的数字,我会得到想要的结果,但是有没有其他方法可以做到呢?在


Tags: canhowpointthreeprintforcemadepercentage
3条回答

你还有一个选择(尽管我不推荐)是使用

from __future__ import division

然后呢

^{pr2}$

这是基于PEP 238。在

你在做整数除法。至少使其中一个成为浮点值

percentage = float(_made) / float(_attempted)

您还可以通过使用新的字符串格式方法获得更好的百分比输出。在

^{pr2}$

将其中一个设为float

float(timothy_lewis_three_pointers_made) / timothy_lewis_three_pointers_attempted

相关问题 更多 >