【问题标题】:How do I get my variables from one function to another?如何将变量从一个函数传递到另一个函数?
【发布时间】:2017-10-02 04:58:41
【问题描述】:
def getInt():
    while True:
        width = int(input("Enter width (1 - 60):"))
        if width > 1 and width < 60:
            break
        else:
            print("Please enter a valid input")
    while True:
        height = int(input("Enter width (1 - 20):"))
        if height > 1 and height < 20:
            break
        else:
            print("Please enter a valid input")
    return width, height


def calcPerimeter(width, height):
    perimeter = (2 * (width + height))
    return perimeter  

def calcArea(width, height):
    area = width * height
    return area


def main():
print('Results: {}'.format(getInt()))
print('The perimeter is {}' .format(calcPerimeter(width, height)))
print('The area is {}' .format(calcArea(width, height)))


main()

当我跑步时,它会说:

Traceback(最近一次调用最后一次):

main()

print('周长是{}' .format(calcPerimeter(width, height)))

NameError: 名称“宽度”未定义

【问题讨论】:

  • 你知道variable scopes吗?你认为width 在main 方法中可见吗?
  • getInt() 返回宽度和高度,但是当您调用 getInt() 时,您不会将返回的值保存在任何地方。试试这个:width, height = getInt().
  • 如果我在 getInt 中打印值,我会将它们作为值返回,我只是无法将它们放入其他函数中。

标签: python python-3.x function


【解决方案1】:

也许你可以试着把你的 main() 改成这个

def main():
  width, height = getInt()
  print('Results: {}, {}'.format(width, height))  
  print('The perimeter is {}' .format(calcPerimeter(width, height)))
  print('The area is {}' .format(calcArea(width, height)))

在传递给另一个函数之前,您需要将 getInt() 返回到特定变量

其他方法是在getInt()函数中调用calculatePerimeter和calculateArea,这里是例子

def getInt():
    while True:
        width = int(input("Enter width (1 - 60):"))
        if width > 1 and width < 60:
            break
        else:
            print("Please enter a valid input")
    while True:
        height = int(input("Enter width (1 - 20):"))
        if height > 1 and height < 20:
        break
        else:
            print("Please enter a valid input")
    return calcPerimeter(width, height), calcArea(width, height)


def calcPerimeter(width, height):
    perimeter = (2 * (width + height))
    return perimeter  

def calcArea(width, height):
    area = width * height
    return area


def main():
  perimeter, area = getInt()
  print('Perimeter is {}, area is {}'.format(perimeter, area))  


main()

【讨论】:

    【解决方案2】:

    一种方法是使用全局关键字。另一种方法是从函数中返回所需的变量并将它们保存在全局范围内。

    【讨论】:

      【解决方案3】:

      您应该阅读这篇文章并总体上研究变量范围的规则。作为程序员,这是必不可少的知识。

      Short Description of the Scoping Rules?

      【讨论】:

        【解决方案4】:

        请参阅此(链接)[http://python-textbok.readthedocs.io/en/1.0/Variables_and_Scope.html] 了解为什么将您的 main 函数更改为 Po 的答案会使代码正确运行。

        函数calcPerimeter 和calcArea 中定义的变量对于main 函数是不可见/不可访问的,因为它们位于不同的范围内。

        【讨论】:

          【解决方案5】:
          #I think i have resolved your problem.In your case it was returning list/tuple .you have to convert it into simple variable
          def getInt():
            while True:
               width = int(input("Enter width (1 - 60):"))
               if width > 1 and width < 60:
                  break
               else:
                  print("Please enter a valid input")
          while True:
              height = int(input("Enter height (1 - 20):"))
              if height > 1 and height < 20:
                  break
              else:
                  print("Please enter a valid input")
          return width,height
          
          
           def calcPerimeter(width, height):
             hw=width+height
             perimeter = 2*hw
             return perimeter  
          
          def main():
            width,height= getInt()
            p=calcPerimeter(width, height)
            print('The perimeter is %s'%p)
            a=calcArea(width, height)
            print('The area is %s'%a)
          
          
          main()
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-12-12
            • 2021-04-23
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多