python - 在我的距离计算器中不明白 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}")
解决方案
您需要将其转换为整数,然后才能进行减法,int(input("x1"))
因为input
返回一个字符串。
推荐阅读
- sql - Bigquery - json_array 来自字段的额外多个元素
- dynamics-crm - 从另一个上下文标记完成
- javascript - 在 react-native 中重新渲染。我们可以优化我面临的问题吗?
- c - 如何允许用户继续输入彩票号码,直到用户决定停止或结束程序。(在 C 编程中)
- excel - VBA代码问题将多个工作表组合成一个主表并在主表存在时刷新
- javascript - Google Apps 脚本占用了太多时间,有没有办法减少运行时间?
- r - 为什么 rowSums 不适用于在 R 中使用 pivot_wider 创建的数据框?
- heroku - 使用 Dockerfile 在 Heroku 上部署 ApacheNifi
- c# - 复合键和外键 EF Core 的关系
- vpn - How to connect to vpn from the another subnet with the same external IP