所以我试图制作一个程序,在列表中选择一个随机对象,然后引用该对象。这是我的代码:for hour in c.routine: a = hour.hour if hour.task == "idle": if c.spouse: if c.spouse[0].routine[a].task == "idle": if hour.hour >= 6 and hour.hour <= 19: x = random.choice(family_daytime_activities) hour.task = x y = hour.hour+1 c.routine[y].task = x c.spouse[0].routine[a].task = x c.spouse[0].routine[y].task = x if c.kids: for k in range(len(c.kids)): if c.kids[k].routine[a].task == "idle": c.kids[k].routine[a].task = x c.kids[k].routine[y].task = x else: x = random.choice(family_nighttime_activities) hour.task = x y = hour.hour+1 c.routine[y].task = x c.spouse[0].routine[a].task = x c.spouse[0].routine[y].task = x elif c.lover: pick = random.choice(c.lover) if c.lover[pick].routine[a].task == "idle": c = random.randint(0,2) if c == 1: if hour.hour >= 6 and hour.hour <= 19: x = random.choice(daytime_activities) hour.task = x y = hour.hour+1 c.routine[y].task = x c.lover[pick].routine[a].task = x c.lover[pick].routine[y].task = x else: x = random.choice(nighttime_activities) hour.task = x y = hour.hour+1 c.routine[y].task = x c.lover[pick].routine[a].task = x c.lover[pick].routine[y].task = x似乎是我尝试提及选秀权的方式,但我不确定为什么或如何解决它...
2 回答
牛魔王的故事
TA贡献1830条经验 获得超3个赞
有很多代码需要梳理。你能举一个更小的例子吗?
根据您的堆栈跟踪,问题出在
if c.lover[pick].routine[a].task == "idle"
好的,我看到了。改变
pick = random.choice(c.lover)
if c.lover[pick].routine[a].task == "idle":
到:
pick = random.choice(c.lover)
if pick.routine[a].task == "idle":
您已经做出了一个随机选择(它包含在 中pick),因此您可以直接使用它。
添加回答
举报
0/150
提交
取消