新来的Python,不知道该怎么对我们

2024-10-04 05:29:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一名高中生,刚刚通过学校被介绍认识python。为了好玩,我做了一个简单的程序来寻找两个y=mx=b公式的交点。但是,除非我在输入后面加上小数点,否则它会对程序的输出进行四舍五入。我试过一些使用浮动的方法,但我不完全理解。代码如下:

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")

我使用的是python2.7。提前谢谢!你知道吗


Tags: oftheto程序inputmakeisline
1条回答
网友
1楼 · 发布于 2024-10-04 05:29:28

当提供两个整数操作数时,Python 2.x(默认情况下)将执行整数除法:

>>> 4 / 3
1

另一方面,Python3.x将执行浮点除法:

>>> 4 / 3
1.3333333333333333

通过强制转换操作数,可以将操作数强制转换为浮点数,在Python2.x中强制执行浮点除法:

>>> float(4) / 3
1.3333333333333333

Python3.x仍然可以执行整数除法,但它使用了一个新的运算符:

>>> 4 // 3
1

相关问题 更多 >