Yes, you should think of defining both your functions in a Class, and making word a member. This is cleaner :
class Spam:
def oneFunction(self,lists):
category=random.choice(list(lists.keys()))
self.word=random.choice(lists[category])
def anotherFunction(self):
for letter in self.word:
print("_", end=" ")
Once you make a Class you have to Instantiate it to an Object and access the member functions
s = Spam()
s.oneFunction(lists)
s.anotherFunction()
Another approach would be to make oneFunction
return the word so that you can use oneFunction
instead of word
in anotherFunction
>>> def oneFunction(lists):
category=random.choice(list(lists.keys()))
return random.choice(lists[category])
>>> def anotherFunction():
for letter in oneFunction(lists):
print("_", end=" ")
And finally, you can also make anotherFunction
, accept word
as a parameter which you can pass from the result of calling oneFunction
>>> def anotherFunction(words):
for letter in words:
print("_",end=" ")
>>> anotherFunction(oneFunction(lists))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…