首页 > 解决方案 > 在我的距离计算器中不明白 TypeError 的原因

问题描述

我是 python 新手,我正在尝试编写一个程序来计算两点之间的距离公式。当我尝试减去表达式时,我在第 11 行不断收到类型错误。我究竟做错了什么?

import math
print("Distance Formula and Slope Calculator")

#store coordinates
x1 = input('x1')
y1 = input('y1')
x2 = input('x2')
y2 = input('y2')

#(x2-x1) and (y2-y1)
expression_1 = x2 - x1
expression_2 = y2 - y1

#square expressions
squared_1 = expression_1**2
squared_2 = expression_2**2

#add expressions
added = squared_1 + squared_2

#square root
distance = math.sqrt(added)

#show distance
print(f"The distance between ({x1}, {y1}) and ({x2}, {y2}) is {distance}")

标签: python

解决方案


您需要将其转换为整数,然后才能进行减法,int(input("x1"))因为input返回一个字符串。


推荐阅读