Home > OS >  Is there a way to pass a random variable defined in one function to another in Python?
Is there a way to pass a random variable defined in one function to another in Python?

Time:10-18

I want to use a variable that chooses between 2 numbers at random, that I get from one function, and use it in another function

def rayuela_extremo1():
    n2 = random.choice([0,1])
    n2 = n2
    print (n2)
    return n2

def rayuela_cuerpo3():
    n2 = rayuela_extremo1()
    if n2 == 0:
        n3 = random.choice([1,3])
        return n3
    elif n2 == 1:
        n3 = random.choice([0,2])
        return n3

The problem is that when I take the variable n2 from my first function and I try to use it in the second one, the value of n2 its choseen at random again so the value of n2 could be 1 in the function rayuela_extremo1 and 0 in rayuela_cuerpo3

CodePudding user response:

Honestly just delete the whole function it has not use. Instead just do

def rayuela_cuerpo3():
    n2 = random.choice([0,1])
    if n2 == 0:
        n3 = random.choice([1,3])
        return n3
    elif n2 == 1:
        n3 = random.choice([0,2])
        return n3
  • Related