【问题标题】:Unsupported operand [closed]不支持的操作数 [关闭]
【发布时间】:2021-09-04 14:52:04
【问题描述】:

我正在尝试使用 python 编写三角形区域,它显示不支持的操作数

height= print(float(input("Enter height ")))
base=print(float(input("Enter base ")))
area=float(height*base)/2
print('Area of triangle is: ', area)

错误信息

面积=浮动(高度*底)/2 类型错误:* 不支持的操作数类型:“NoneType”和“NoneType”

【问题讨论】:

  • 你不需要打印input语句
  • print 将其参数打印到屏幕并返回None,因此您的前两个分配使None
  • 您将print 分配给heightbase。只需使用height = float(input("Enter height "))

标签: python python-3.8


【解决方案1】:

你应该删除前两行中的print(),因为它返回一个None,你不能添加它,-,x,/等等...所以删除它-

height = float(input("Enter height "))
base = float(input("Enter base "))
area = (height*base)/2
print('Area of triangle is: ', area)

编辑 - 您可以删除该区域的浮动调用,因为无论如何它都会是浮动的。

【讨论】:

  • 请注意,area 的定义中对@9​​87654324@ 的调用也是不必要的;两个floats 的乘积是floatfloat 除以intfloat
  • 是的,你是对的
猜你喜欢
  • 1970-01-01
  • 2015-03-27
  • 2020-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多