How can I pass global variables into a function?

How to pass data in python3 variables to another function?

  • How would I store and pass the contents of r from main into two? It will print r if I make it r = something fixed, but how do I pass and hold variable contents into r. I tried random.getstate/setstate but it was saying "function object not subscriptable". import random from random import randrange def main(): r = random.randrange(1,13,1) print (r) r = random.randrange(1,13,1) def two(): if r==7 or r==11: print (r) else: print (r) main() two()

  • Answer:

    Something like this should work #!/usr/bin/env python3 import random def main(): r= random.randrange(1,13,1) print(r) return r def two(r): if r == 7 or r == 11: print(r) else: print(r) two(main()) if you are not supposed to pass arguments, you could use a global variable. I'm not fond of that though.

meganree... at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.