-1

I am a high school student who has just been introduced to python through school. Just for fun, I made a simple program for finding the point of intersection of two y=mx=b formulas. However, unless I put decimal points after the inputs, it rounds the output of the program. I have tried some things using floats but I don't fully understand it. Here is the code:

print("When inputting numbers, please make sure to add two decimal points at     the end to make sure answer is not rounded")
x_one=input("Slope of line 1:  ")
y_one=input("Y intercept line 1:  ")
x_two=input("Slope of line 2:  ")
y_two=input("Y intercept 2:  ")
eliminator_1=(x_one*x_two)
eliminator_2=(y_one*x_two)
eliminator_3=(y_two*x_two)
eliminator_4=(y_two*x_one)
print(eliminator_1, eliminator_2, eliminator_3, eliminator_4)
formula_ypro=(eliminator_2-eliminator_4)
formula_y=formula_ypro/(x_two-x_one)
print(formula_y)
formula_x=((formula_y-y_one)/x_one)
print("the point of intersection is", formula_x, formula_y)
h=input("Press Enter To Exit")

I am using python 2.7. Thanks in advance!

1 Answers1

2

Python 2.x will (by default) perform integer division when supplied with two integer operands:

>>> 4 / 3
1

Python 3.x, on the other hand, will perform floating-point division:

>>> 4 / 3
1.3333333333333333

You can coerce an operand into a float by casting it, forcing floating-point division in Python 2.x:

>>> float(4) / 3
1.3333333333333333

Python 3.x can still perform integer division, but it uses a new operator:

>>> 4 // 3
1
Nathan Osman
  • 71,149
  • 71
  • 256
  • 361
  • I guess the other piece of the puzzle is that `input` will return an integer or a float depending upon whether you include a decimal point. – Peter Wood Feb 25 '17 at 23:37
  • Use of `input` is generally discouraged since it interprets the input as Python code, rather than as a string. `raw_input` should be used instead. – Nathan Osman Feb 25 '17 at 23:38
  • 1
    Note that you can vote to close extremely common duplicate questions such as this one. This helps to reduce fragmentation on the site and make things easier to find. – TigerhawkT3 Feb 26 '17 at 00:16