はい、クラス内で両方の関数を定義し、単語をメンバーにすることを考えてください。これはきれいです
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=" ")
クラスを作成したら、それをオブジェクトにインスタンス化してメンバー関数にアクセスする必要があります。別のアプローチは、あなたがanotherFunction
>>> def oneFunction(lists):
category=random.choice(list(lists.keys()))
return random.choice(lists[category])
>>> def anotherFunction():
for letter in oneFunction(lists):
print("_",end=" ")
そして最後に、あなたはまた、どのパラメータとして言葉を受け入れ、anotherFunction
を作ることができるで代わりに単語のoneFunction
を使用できるように言葉を返すoneFunction
作ることであろう
s = Spam()
s.oneFunction(lists)
s.anotherFunction()
あなたはoneFunction
>>> def anotherFunction(words):
for letter in words:
print("_",end=" ")
>>> anotherFunction(oneFunction(lists))
'anotherFunction'の引数として' word'を渡さないのはなぜですか? 'def anotherFunction(word):'を考えてそれに応じて呼び出す。 –