【问题标题】:TypeError: unsupported operand type(s) for %: 'NoneType' and 'float'TypeError:不支持的操作数类型 %:“NoneType”和“float”
【发布时间】:2017-06-06 22:15:59
【问题描述】:
if 1:
T1=300
P1=1
h1=462
s1=4.42
hf=29
sf=0.42
print("The inlet conditions to compressor are 1atm pressure & 300K")
Wi=(T1*(s1-sf))-(h1-hf)
P2= input("What is the final compression pressure? ")
T2= input("What is the final compression temperature? ")
h2= float(input("From graph, enthalpy at point 2 is "))
s2= float(input("From graph, entropy at point 2 is "))
y= float((h1-h2)/(h1-hf))
W= float((T1*(s1-s2))-(h1-h2))
Wf= float(W/y)
FOM= float(Wi/Wf)
print("")
print("Yield= %f") %(y)
print("Work reqd per unit mass of gas compressed= %f KJ/kg") %(W)
print("Work reqd per unit mass of gas liquified= %f KJ/kg") %(Wf)
print("Figure of Merit= %f") %(FOM)

压缩机的入口条件是 1atm 压力和 300K 最终压缩压力是多少? 20 最终压缩温度是多少? 300 从图中,点 2 的焓为 432 从图中,点 2 的熵为 2.74

产量= %f 回溯(最近一次通话最后): 文件“”,第 19 行,在 print("产量 = %f") %(y) TypeError: 不支持的操作数类型 %: 'NoneType' 和 'float'

【问题讨论】:

  • print 函数返回None,然后您将% 运算符应用到它,其值为y,即float,因此您得到错误@987654327 @

标签: python python-3.x


【解决方案1】:

您的问题出在代码的最后 4 行。您需要在这些语句中的每个表达式周围加上括号 ()。您不能在print 函数上应用%,因为它返回None

最后 4 行代码应该是这样的

print(("Yield= %f") % (y))
print(("Work reqd per unit mass of gas compressed= %f KJ/kg") %(W))
print(("Work reqd per unit mass of gas liquified= %f KJ/kg") %(Wf))
print(("Figure of Merit= %f") %(FOM)) 

【讨论】:

    【解决方案2】:

    您应该在字符串上应用% 格式,但目前您在None 上应用它(print 函数返回的值)。为了使您的代码正常工作,您应该这样做:

    print("Yield= %f" % (y)) 
    #                 ^ moved inside `(...)` of print
    

    代替:

    print("Yield= %f") %(y)
    

    【讨论】:

      猜你喜欢
      • 2014-03-28
      • 2018-06-20
      • 1970-01-01
      • 1970-01-01
      • 2014-05-10
      • 1970-01-01
      • 2020-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多