首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >.ceil()数学函数不工作?

.ceil()数学函数不工作?
EN

Stack Overflow用户
提问于 2013-10-03 03:52:40
回答 2查看 1.1K关注 0票数 0

Python解释器说paintRequiredCeiling是未定义的。我在代码中找不到任何错误。程序的目标是从用户那里获取输入,然后计算喷漆作业所需的成本/小时。

代码语言:javascript
复制
import math

def main():
    # Prompts user for sq and paint price
    totalArea = float(input("Total sq of space to be painted? "))
    paintPrice = float(input("Please enter the price per gallon of paint. "))

    perHour = 20
    hoursPer115 = 8

    calculate(totalArea, paintPrice, perHour, hoursPer115)
    printFunction()

def calculate(totalArea, paintPrice, perHour, hoursPer115):
    paintRequired = totalArea / 115
    paintRequiredCeiling = math.ceil(paintRequired)
    hoursRequired = paintRequired * 8
    costOfPaint = paintPrice * paintRequiredCeiling
    laborCharges = hoursRequired * perHour
    totalCost = laborCharges + costOfPaint

def printFunction():
    print("The numbers of gallons of paint required:", paintRequiredCeiling)
    print("The hours of labor required:", format(hoursRequired, '.1f'))
    print("The cost of the paint: $", format(costOfPaint, '.2f'), sep='')
    print("Total labor charges: $", format(laborCharges, '.2f'), sep='')
    print("Total cost of job: $", format(totalCost, '.2f'), sep='')

main()
EN

回答 2

Stack Overflow用户

发布于 2013-10-03 03:57:30

变量paintRequiredCeiling仅在计算函数中可用。它不存在于你的printFunction中。与其他变量类似。您需要将它们移到函数之外,或者传递它们,才能使其正常工作。

票数 1
EN

Stack Overflow用户

发布于 2013-10-03 03:58:05

calculate()函数中没有return语句:计算所有这些值,然后在函数结束时丢弃它们,因为这些变量都是函数的局部变量。

类似地,您的printFunction()函数不接受任何要打印的值。所以它期望变量是全局的,因为它们不是全局的,所以你会得到你得到的错误。

现在您可以使用全局变量,但这通常是错误的解决方案。相反,学习如何使用return语句返回calculate()函数的结果,将这些结果存储在main()的变量中,然后将它们传递给printFunction()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19145645

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档