TypeError:不支持的操作数类型 -:python 中的“str”和“str”[重复]

2023-12-29

我已经编写了这个简单的脚本,但是当它到达数学部分时它给了我一个错误。我尝试将其设置为 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()

编辑:我意识到我的错误,很抱歉造成混乱:(


你需要转换str到要执行的数字类型-对他们的操作:

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

TypeError:不支持的操作数类型 -:python 中的“str”和“str”[重复] 的相关文章

随机推荐