-1

How can I store and pass the contents of r from main to two? It will print r if I set it to a fixed value; but how do I do it with variable contents? 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()
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
user1316212
  • 21
  • 1
  • 1
  • 4
  • How did you find out about `random.getstate`? That's totally unrelated. The way you pass information between functions is the same no matter what the functions do, and totally unrelated to the fact that you're using the `random` module, so I don't understand why you would go digging around in there to find something that might help. These are programming fundamentals that you should be learning first, before trying to work with others' code (by importing modules). Yes, that means writing extremely limited things at first. – Karl Knechtel Apr 06 '12 at 22:03
  • 1
    @KarlKnechtel I actually wrote that code myself, and I am taking a Python programming class right now so I really don't appreciate the assumption that I was taking someone else's code when what I did was look in the python docs. Next time, think before you assume. It is offensive. – user1316212 Apr 13 '12 at 22:53
  • I made no such assumption, and don't understand how that comment relates to my own. – Karl Knechtel Aug 15 '22 at 02:36

2 Answers2

3

Well, depending on the situation, you either want to make a class or return the data, then pass it in as an argument, the latter is probably what you want given your code, so, for example:

def main():
  ...
  r = do_something()
  ...
  return r

def two(r):
  ...
  do_something_else(r)
  ...

r = main()
two(r)

If this was happening within something you could define as an entity, you could do it in a class:

class Main():
  def main(self):
    ...
    self.r = do_something()
    ...

  def two(self):
    ...
    do_something_else(self.r)
    ...

main = Main()
main.main()
main.two()

However, in this case, this is unnecessary as you are creating a class that doesn't really encapsulate anything.

Gareth Latty
  • 86,389
  • 17
  • 178
  • 183
2

Use return values and function parameters:

import random
from random import randrange

def main():
    r = random.randrange(1,13,1)
    return r

def two(r):
    if r==7 or r==11:
       print (r)
    else:
       print (r)

r = main()
two(r)

The function main computes the random value and then returns it to the place where it was called. You can then store the return value in a variable (also called r in this case: r = main()). It does not need to be called r though, you can also name it something else.

To pass the value to two, you can pass it as parameter. As you can see in def two(r):, the function two now expects a parameter (also called r) and it is used in that function. Again, this does not need to be called r but it seems fitting to use the same name.

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180