-5

I want to use a function's variable which is [a] in here to another function, is it possible with global variables or class? if yes how

 def apple():
     a = 2
 def orange():
     if a == 2:
         print('your fruit is apple')

i'm not lazy,i spent time to find the answer but i couldn't,so understand my low skill programming.

Makaveli
  • 9
  • 5
  • pass `a`as a parameter to the other function? e.g. `def other_function(a):..`? which you can call from your functions as `other_function(a)` – chickity china chinese chicken Nov 05 '18 at 18:18
  • read [PyTut: defining functions](https://docs.python.org/3/tutorial/controlflow.html#defining-functions) and [PyTut: More on defining functions](https://docs.python.org/3/tutorial/controlflow.html#more-on-defining-functions) – Patrick Artner Nov 05 '18 at 18:20
  • 1
    and for good measure: [Description of the scoping rules](https://stackoverflow.com/questions/291978/short-description-of-the-scoping-rules) – Patrick Artner Nov 05 '18 at 18:20
  • 2
    This might seem like too subtle a distinction, but you want to use the *value* of the `apple`'s variable `a` in the function `orange`, not the variable itself. – chepner Nov 05 '18 at 18:21

1 Answers1

0

Put it into a class like:

class fruit:
    a = 0
    def apple(self):
            self.a = 2
    def orange(self):
            if(self.a == 2):
                print('your fruit is apple')
a = fruit()
Tobi696
  • 448
  • 5
  • 16