【问题标题】:Error for fencing calculation program [duplicate]围栏计算程序错误[重复]
【发布时间】:2018-01-22 19:23:44
【问题描述】:

我的目标是在 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: Can't convert 'int' object to str 隐式错误代码。 如果您能用非常简单的术语解释这一点,我将不胜感激。 谢谢:)

【问题讨论】:

  • width = 语句中缺少两个右括号。
  • perMetre 应转换为整数或浮点数。 width 也是如此。也就是说,如果您使用的是 python 3

标签: python


【解决方案1】:

请参阅下面替换代码中的 cmets。

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

【讨论】:

  • 非常感谢你们的帮助。现在都在工作。继续努力:)
猜你喜欢
  • 2017-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-05
  • 2021-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多