我正在为一个游戏制作一个基因模拟器,当我调用我的函数来填充决定父母基因的数组时,它会为每个父母提供相同的输出
输出通常是这样的。 ruby=[G,U,B,N,N,G] jaune=[G,U,B,N,N,G]
我想要的输出是这样的。 ruby=[R,A,N,R,N,B] jaune=[W,N,W,N,na]
import random
import time
class Parent():
magic = [None, None, None, None, None, None]
types = ['B', 'U', 'W', 'R', 'G', 'N', 'A']
def gen(self):
for i in range(0, 6):
self.magic[i] = self.types[random.randint(0, 6)]
def traits(self):
print (self.magic)
jaune = Parent()
ruby = Parent()
jaune.gen()
ruby.gen()
jaune.traits()
ruby.traits()
使用构造函数和self值。您的
magic
是一个全局变量此外,还可以在
__init__
内生成值检查
self
相关问题 更多 >
编程相关推荐