TypeError:-:python中的“ str”和“ str”不受支持的操作数类型

时间:2018-12-04 00:23:25

标签: python

我已经编写了这个简单的脚本,但是在进入数学部分时却给了我一个错误。我尝试将其设置为int(),但这也不起作用。

import os
from fractions import Fraction
def cls():
    os.system('cls' if os.name=='nt' else 'clear')
def pause():
    programPause = raw_input("Press the <ENTER> key to continue...")

def header():
    print("Slope Calculator")
    print("Daniel Meskin")
    print("BSD Licen    ce")
    print("<------------------    >")

header()
x1 = input("(")
gcoord="("+x1+","
cls()
header()
y1 = input(gcoord)
gcoord="("+x1+","+y1+"),("
cls()
header()
x2 =input(gcoord)
gcoord="("+x1+","+y1+"),("+x2+","
cls()
header()
y2 = input(gcoord)
gcoord="("+x1+","+y1+"),("+x2+","+y2+")"
cls()
header()
print(gcoord)
slope = (y2-y1)/(x2-x1)
slopef=Fraction(slope)
print(slope+"|"+slopef)
pause()

编辑:我意识到自己的错误,对不起您的困惑:(

1 个答案:

答案 0 :(得分:3)

您需要将str转换为数字类型才能对其执行-操作:

slope = (float(y2)-float(y1))/(float(x2)-float(x1))

(或者您可以根据需要使用int()代替float()