Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make python print 1 as opposed to 1.0

Tags:

python

int

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.")
like image 976
Charlie Kimzey Avatar asked Oct 27 '12 03:10

Charlie Kimzey


2 Answers

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))
like image 126
Blender Avatar answered Oct 29 '22 23:10

Blender


Try this,

> x = 10.0
> print int(x)
10
> print x
> 10.0

Is this what you are really looking for?

like image 23
kumar_m_kiran Avatar answered Oct 29 '22 22:10

kumar_m_kiran