Cannot call a variable from another function

后端 未结 4 719
陌清茗
陌清茗 2021-01-24 08:07

I edited my thread based on feedback. Basically, I need to use a couple of variables from function 1, and I need to print it in function 2.

How do I go about doing that?

4条回答
  •  醉梦人生
    2021-01-24 08:28

    OK, so your variables are caught inside the scope of the function. To use them outside that scope, you need to return them out, e.g.:

    def function_one():
      number_one = 5
      number_two = 6
      number_three = 7
      return number_one,number_two, number_three
    
    def function_two(number1, number2, number3):
      print(number1)
      print(number2)
      print(number3)
    
    one, two, three = function_one()
    function_two(one, two, three)
    

    and here I have made the various vars different in their naming in their different scopes to make it all more apparent.

提交回复
热议问题