python - How to pull a variable from one function to another -


    def letterchoice():         playerletter = input('please choose x or o.').upper()         if playerletter in ['x','o']:           print('the game begin.')         while playerletter not in ['x','o']:           playerletter = input('choose x or o.').upper()         if playerletter == 'x':           computerletter = 'o'         else:           computerletter = 'x'         turnchooser()     def turnchooser():         choice = input("would go first, second or decide coin toss?(enter 1, 2 or c) ")         while choice not in ["1","2","c"]:           choice = input("please enter 1, 2 or c. ")         if choice == 1:           print("g")           cur_turn = letterchoice.playerletter()         elif choice == 2:           print("h")         else:           print("p")         movetaker() 

i can't figure out how i'm supposed inherit playerletter turnchooser(), i've tried putting playerletter brackets of each function don't pass , create argument error , print("g") , on there see if code works whenever enter 1 or 2 "p" outputted.

you need define function attributes playerlatter

for ex:

def foo():     foo.playerletter=input('please choose x or o.').upper()   >>> foo() please choose x or o.x  >>> foo.playerletter 'x' 

accessing function

def bar():     variable=foo.playerletter     print(variable)   >>> bar() x >>>  

you can check attributes available given function

>>> [i in dir(foo) if not i.startswith('_')] ['playerletter'] >>>  

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -