防护计算程序出错

时间:2017-08-14 21:24:12

标签: python

我的目标是在python中创建一个程序,允许用户输入每米围栏的成本,围场的长度和宽度,程序应该计算并打印周长,然后是报价。我遇到了一些问题,遗憾的是我对Stack Overflow的研究没有成功。我的代码如下(请记住,我只是一个初学者)

    from turtle import*
print ("Welcome to the fencing quote calculator")
print ("In this program, you will be able to input the size of the padock as well as the cost of the fence per meter")
again = ""
perMetre = input(str(("Please input the cost per metre in numbers without a dollar sign")
width = input("Please input the width of the paddock in metres")
length = input("Please input the length of the paddock in metres")

perimeter = (width+2)
print ("The perimeter of this paddock is",perimeter)
cost = int((perimeter)*(perMetre))
print("{:.2f}".format(cost))

错误是TypeError:无法转换' int'对象str隐式错误代码。 如果你能用非常简单的术语解释这一点我会非常感激。 谢谢:))

1 个答案:

答案 0 :(得分:0)

请参阅以下替换代码中的评论。

from turtle import*
print ("Welcome to the fencing quote calculator")
print ("In this program, you will be able to input the size of the padock as well as the cost of the fence per meter")
again = ""
perMetre = input(str(("Please input the cost per metre in numbers without a dollar sign")
width = input("Please input the width of the paddock in metres")
length = input("Please input the length of the paddock in metres")

# need to convert "numbers" currently input as strings to floating point numbers
perMetre = float(perMetre)
width = float(width)
length = float(length)

# also fix perimeter equation?
#perimeter = (width+2)
perimeter = (width + length) * 2.0
print ("The perimeter of this paddock is",perimeter)
cost = perimeter * perMetre
print("{:.2f}".format(cost))