python - 删除范围为 .01 到 .99 的所有 float

标签 python

我有一个简单的python程序

除法后,它显示最终值,但我不想显示 .01

from __future__ import division

number = int(133)
output = float(0)
divideNumber = int(1)

stop = false

while stop == false
   halfNumber = number / 2
  output =  number / divideNumber 
  output = round(output, 2)

  if ".0" in str(output):
    if "0.1" in str(output) or "0.2" in str(output ) or.... "0.9" in str(output): 
      #Do Nothing
  else: 
      #Do Nothing
    else: 
      print str(number) + " / " + divideNumber + " = "str(output)

  divideNumber += 1

  if divideNumber < halfNumber:
    break
  else: 
    #Do Nothing
print "Goodbye"

如果我运行它,结果是这样的:

133 / 1 = 133.0
133 / 7 = 19.0
133 / 11 = 12.09
133 / 12 = 11.08
133 / 19 = 7.0
133 / 22 = 6.05
133 / 33 = 4.03
133 / 43 = 3.09
133 / 44 = 3.02
133 / 64 = 2.08
133 / 65 = 2.05
133 / 66 = 2.02

再见

我的预期结果是

 133 / 1 = 133.0
 133 / 7 = 19.0
 133 / 19 = 7.0
 Goodbye

我的“if”语句错了吗?我没有收到任何错误!

最佳答案

“我有一个简单的 python 程序”

这不是一个简单的程序。

如果你想用一位小数显示结果,使用这个:

print '{:.1f}'.format(133./19.)

这打印

7.0

如果你想测试一个整数是否能整除另一个:

if not x%y:
    # y divides x

关于python - 删除范围为 .01 到 .99 的所有 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14274195/

相关文章:

python - pandas' `str.extract()` 中带有捕获组的正则表达式的预期行为

python - 打印时指定 Pandas DataFrame 列之间的空格数

python - Python xml.dom 中的非递归(单节点级别)getElementsByTagName

python - Pipenv 警告 : Invalid requirement, 解析错误在 "' --extra -'"

python - pyparsing setParseAction 没有传递 token

python - 情节离线 : Generate bar chart of 2 columns on (y axis) & 1 column on ( x axis)

python - 在 Python Tkinter 中使用函数时添加图像

python - Mypy 无法推断从列表变量创建的枚举

python - 如何从 Python 日期时间获取可识别时区的年、月、日、小时等?

python - 在 Cython 中将复杂的 numpy 数组传递给 C++