我怎样才能让我的程序计算三角形的面积?在 Python

How can I get my program to calculate the area of triangles? In Python

到目前为止,这是我的程序,有人可以告诉我我做错了什么或给我一个修复程序吗?全部在 python 中,非常感谢您的回答。弹出错误消息说 "Can't multiply sequence by non-int of type 'str'".

height = input("What is the height of the triangle? ")
width = input("What is the width of the triangle? ")

area = width * height /2

print("The area of the triangle would be {0:f} ".format(area))

您正在使用 input 获取号码,但从 input 方法返回的类型是字符串。将其转换为 int 即可。

示例:

area = int(width) * int(height) /2

实际上您使用的输入 height = input("What is the height of the triangle? ") 不是整数,这里是您可以解决的问题

height = eval(raw_input("What is the height of the triangle? "))
width = eval(raw_input("What is the width of the triangle?"))
area = width * height /2
print("The area of the triangle would be {0:f} ".format(area))

您应该将您的值转换为 float 数据类型:

height = float(input("What is the height of the triangle? "))
width = float(input("What is the width of the triangle? "))

area = width * height /2

print("The area of the triangle would be {0:f} ".format(area))