首页 > 解决方案 > 不能连接 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。我该如何解决?

标签: pythonpython-3.xinput

解决方案


您必须在连接之前将其转换为字符串


推荐阅读