我收到以下错误消息全局名称错误消息

当我尝试运行以下程序时,我收到以下消息:Builtins.NameError:未定义全局名称'GALONS'

选择 | 换行 | 行号
  1. def main():
  2.     square_feet = float(input("Enter the square feet of the wall that is to be painted: "))
  3.     gallons = (square_feet/115)
  4.     paint = float(input("Enter the cost of paint per gallons: "))
  5.     labor = (gallons * 8 * 20)
  6.     price_paint = (paint + gallons)
  7.     hours = (square_feet/14.37)
  8.     total_cost = (labor + price_paint)
  9.     charges()
  10. def charges():
  11.     print ("The number of gallons of paint is", gallons)
  12.     print ("The number of hours of labor is",hours)
  13.     print ("The cost of the paint is",price_paint)
  14.     print ("The cost for labor will be",labor)
  15.     print ("The total cost for the paint job is", total_cost)
  16. main()
# 回答1


概念的定义
加仑
对于功能而言是本地的
主()
而不是在函数的命名空间中
收费()
。有关Python作用域规则的更多信息,请参见
Python文档9.2。Python作用域和命名空间

# 回答2


好的,那你怎么用两个函数来做呢?
# 回答3


使用创建全局变量
全球
语句或将值传递给函数
收费()
作为论据。

标签: python

添加新评论