Search code examples
pythonint

How to make python print 1 as opposed to 1.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.")

Solution

  • Since you're dividing integers, Python represents the result as a float, not as an int. To format the floats 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))