【问题标题】:cannot concatenate str and float [duplicate]无法连接 str 和 float [重复]
【发布时间】:2019-10-03 00:20:57
【问题描述】:

当我打印时,它说我不能连接 float 和 str。我该如何解决这个问题?

shape = input("你想选择什么形状:圆形?三角形?矩形?还是五边形?") 饼图 = 浮点数(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 shape == "矩形":

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。我该如何解决?

【问题讨论】:

  • 你搜索错误信息了吗?您尝试了哪些解决方案?
  • 我认为可以通过用逗号替换所有“+”来修复它(在打印功能中)。

标签: python python-3.x input


【解决方案1】:

你必须在连接之前把它变成一个字符串

【讨论】:

    猜你喜欢
    • 2013-06-01
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-01
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    相关资源
    最近更新 更多