python - 不能连接 str 和 float
问题描述
当我打印时,它说我不能连接 float 和 str。我该如何解决?
shape = input("你想选择什么形状:圆形?三角形?矩形?还是五边形?") pie = float(3.141592)
如果形状 == “圆形”:
radius = float(input("what is the radius of the circle?"))
area = pie * (radius * radius)
perimeter = radius * pie * 2
print("the area of this circle is " + area)
print(" the circumference of this circle is: " + perimeter)
elif 形状 == “三角形”:
side_1 = float(input("input the value of one of the sides"))
side_2 = float(input("input the value of one of the sides"))
base = float(input("what is the base? "))
tri_perimeter = side_1 + side_2
print("the perimeter of this triangle is " + tri_perimeter)
height = float(input("what is the height? "))
tri_area = (base * height)/2
print("the area of the triangle is " + tri_area)
elif 形状 == “矩形”:
base = float(input("what is the base? "))
height = float(input("what is the height? "))
rec_area = base * height
rec_perimeter = (base * 2) + (height * 2)
print("the area of the rectangle is " + rec_area)
print("the perimeter is " + rec_perimeter)
否则:打印(“错误!”)
我真正苦苦挣扎的部分是当我想打印一些东西(9面积或周长)时。它说我不能连接 str 和 float。我该如何解决?
解决方案
您必须在连接之前将其转换为字符串
推荐阅读
- api - 如何在flutter中调用initState中的异步方法
- python - 字典列表的差异
- django - 将父对象传递给子对象的 CreateView
- environment-variables - 如何通过设置环境变量的 Windows 终端启动 Git Bash?
- r - 如何在 R 中为多列创建分层采样
- excel - 从 GitHub 下载 CSV 文件
- utf-8 - 打印 UTF8 编码的笑脸
- javascript - 如何检测路由器路径“/”何时为真?
- javascript - 使用 ng-select 显示所选选项的计数
- python - 当我选择微调器值(即官方)以及何时选择微调器值正常隐藏标签时,如何创建标签?