如何制作python print 1而不是1.0

Cha*_*zey 2 python int

我正在制作一个数学求解程序,它将整数打印为小数.像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.")
Run Code Online (Sandbox Code Playgroud)

Ble*_*der 7

因为你是整数,所以Python将结果表示为a float,而不是int.要根据需要格式化floats,您必须使用字符串格式:

>>> print('{:g}'.format(3.14))
3.14
>>> print('{:g}'.format(3.0))
3
Run Code Online (Sandbox Code Playgroud)

所以将它插入你的代码:

print("Y = {:g}X - {}.".format(m, b))
Run Code Online (Sandbox Code Playgroud)