moa*_*eey 1 python syntax types
当我运行我的脚本时,我得到了一个TypeError.这是我的所有代码:
lawnCost = ("£15.50")
lengthLawn = float(input("Lengh of lawn: "))
widthLawn = float(input("Width of lawn: "))
totalArea = (lengthLawn) * (widthLawn)
print (("The total area of the lawn is ")+str(totalArea)+str("m²"))
totalCost = (totalArea) * float(15.50)
print ("The cost of lawn per m² is £15.50")
print ("The total cost for the lawn is ")+str(totalCost)
这是我得到的错误:
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
如果有人能帮我指出正确的方向,那就太棒了,谢谢.
另外,如果它有助于我在Windows 7 x64上运行Python 3.3.
在最后一行,str(totalCost)必须是内部括号为print:
print ("The total cost for the lawn is "+str(totalCost))
这是因为Python 3.x中的print返回None.所以,你的代码实际上是在尝试这样做:
None+str(totalCost)
此外,如果您需要它,下面是您的脚本版本,它更清洁,更高效:
lawnCost = "£15.50"
lengthLawn = float(input("Lengh of lawn: "))
widthLawn = float(input("Width of lawn: "))
totalArea = lengthLawn * widthLawn
print("The total area of the lawn is {}m²".format(totalArea))
totalCost = totalArea * 15.50
print("The cost of lawn per m² is £15.50")
print("The total cost for the lawn is {}".format(totalCost))
基本上,我做了三件事:
删除不必要的括号和后面的额外空格print.
删除了对str和的不必要的电话float.
合并使用str.format.