它一直说我的函数没有在python中定义

时间:2014-09-25 23:24:50

标签: python

我正在参加计算机编程课程,在其中我们使用python。

我的任务是编写一个名为paint.py的程序,该程序将确定用矩形地板绘制棚屋墙壁的成本。假设棚子没有窗户,油漆每加仑40美元。一加仑占地300平方英尺。提示用户输入棚屋的尺寸。使用名为paint_cost的函数将用户输入作为参数,并返回绘制棚屋墙壁的成本。以货币格式表示费用。

我一直在努力思考如何去做。我在我的python书中一遍又一遍地研究和阅读这一章。所以,如果有人能帮助我。

def paint_cost(price):
    return (dimen / 300) * 40
def main():
    dimen = input('Enter the dimensions of the shed: ')
    print('Your cost of painting will be $', paint_cost(price))
main()

3 个答案:

答案 0 :(得分:1)

原始代码中的错误:

  • 第2行:retun应为return
  • price功能中删除paint_cost,因为您是在第2行计算的。{/ li>
  • 将其替换为dimen

def paint_cost(dimen):
    cost = (dimen / 300) * 40
    return cost
def main():
    dimen = int(input('Enter the dimensions of the shed: '))
    print 'Your cost of painting will be $ %s' % str(paint_cost(dimen))
main()

答案 1 :(得分:1)

我认为这更接近你要做的事情:

def paint_cost(dimen):
    return (dimen / 300.) * 40  # calculates cost based on dimension
def main():
    dimen = int(input('Enter the dimensions of the shed: ')) ]  # cast input as an integer
    print('Your cost of painting will be ${}'.format(paint_cost(dimen)))# pass the dimensions to paint_cost and print using `str.format`
main()

原始代码中的错误:

retun应为return,因此语法错误

(dimen / 300) * 40 dimen仅存在于main函数中,因此未定义错误

paint_cost(price)价格未定义,因此另一个未定义的错误

答案 2 :(得分:-1)

在你的行中:

print('Your cost of painting will be $', paint_cost(price))

price尚未定义。

通常Python会很好地描述出错的地方,就像我在这里运行时一样:

NameError: global name 'price' is not defined

还有其他问题,但要按照自己的方式进行,特别注意追溯。