繁体   English   中英

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

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

到目前为止,这是我的程序,有人能告诉我我做错了什么或给我一个修复吗? 这一切都在python中,我真的很感激答案。 错误消息弹出说“不能将序列乘以'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

您应该将您的值转换为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))

实际上,您使用的输入 height = input("三角形的高度是多少?") 不是整数,这是您可以修复的

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))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM