Tuesday, April 24, 2012

Calling variable defined inside one function from another function. PYTHON 3.2.3

if I have this:



def oneFunction(lists):
category=random.choice(list(lists.keys()))
word=random.choice(lists[category])

def anotherFunction():
for letter in word: #problem is here
print("_",end=" ")


I have previously defined lists, so oneFunction(lists) works perfectly.



My problem is calling word in line 6. I have tried to define word outside the first function with the same word=random.choice(lists[category]) definition, but that makes word always the same, even if I call oneFunction(lists).



I want to be able to, every time I call the first function and then the second, have a different word.



Can I do this without defining that word outside the oneFunction(lists)?





No comments:

Post a Comment