如何使python打印1而不是1.0 [英] How to make python print 1 as opposed to 1.0
本文介绍了如何使python打印1而不是1.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在制作一个数学求解程序,它始终将整数打印为小数.像1是1.0,5是5.0,我的代码是:
I am making a math solving program, it keeps printing the whole numbers as decimals. Like 1 is 1.0, 5 is 5.0, and my code is:
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
的格式,您必须使用字符串格式:
Since you're dividing integers, Python represents the result as a float
, not as an int
. To format the float
s as you'd like, you'll have to use string formatting:
>>> print('{:g}'.format(3.14))
3.14
>>> print('{:g}'.format(3.0))
3
因此将其插入您的代码中
So plugging it into your code:
print("Y = {:g}X - {}.".format(m, b))
这篇关于如何使python打印1而不是1.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文