我正在制作一个数学求解程序,它不断将整数打印为小数。就像1是1.0,5是5.0,我的代码是:
print("Type in the cooridinates of the two points.")
print("")
print("---------------------")
print("First point:")
x1 = int(input("X: "))
y1 = int(input("Y: "))
print("")
print("---------------------")
print("Second point:")
x2 = int(input("X: "))
y2 = int(input("Y: "))
m = (y1-y2) / (x1-x2)
b = y1 - m * x1
round(m, 0)
round(b, 0)
print("Completed equation:")
print("")
if b < 0:
print("Y = "+ str(m) +"X - "+ str(b) +".")
elif b > 0:
print("Y = "+ str(m) +"X + "+ str(b) +".")
elif b == 0:
print("Y = "+ str(m) +"X.")
input("Press enter to continue.")
由于您要除以整数,Python 将结果表示为float
,不作为int
。格式化float
如您所愿,您必须使用字符串格式:
>>> print('{:g}'.format(3.14))
3.14
>>> print('{:g}'.format(3.0))
3
因此,将其插入您的代码中:
print("Y = {:g}X - {}.".format(m, b))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)