在列表中选择一个随机对象

时间:2018-10-24 15:38:17

标签: python

所以我试图制作一个程序,该程序在列表中选择一个随机对象,然后引用该对象。

这是我的代码:

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

运行此代码时出现错误:

Traceback (most recent call last):   File
"C:\Users\Patrick\Pictures\Python\Westhope\2.0\exe.py", line 9, in
<module>
    routine_creation()   File "C:\Users\Patrick\Pictures\Python\Westhope\2.0\world_init.py", line
721, in routine_creation
    if c.lover[pick].routine[a].task == "idle": TypeError: object cannot be interpreted as an index

似乎是我尝试提及选秀权的方式,但我不确定为什么或如何解决它...

2 个答案:

答案 0 :(得分:1)

如果您还需要索引,请使用random.randrange

 var express = require('express');
var mongoose = require('mongoose');
var passport = require('passport');
var config = require('../config/database');
require('../config/passport')(passport);
var express = require('express');
var jwt = require('jsonwebtoken');
var router = express.Router();
var User = require("../models/user");
var Book = require("../models/book");
var router = express.Router();



  router.post('/books', passport.authenticate('jwt', { session: false}), function(req, res) {
    var token = getToken(req.headers);
    if (token) {
      console.log(req.body);
      var newBook = new Book({
        isbn: req.body.isbn,
        title: req.body.title,
        author: req.body.author,
        publisher: req.body.publisher
      });

      newBook.save(function(err) {
        if (err) {
          return res.json({success: false, msg: 'Save book failed.'});
        }
        res.json({success: true, msg: 'Successful created new book.'});
      });
    } else {
      return res.status(403).send({success: false, msg: 'Unauthorized.'});
    }
  });

  router.get('/books', passport.authenticate('jwt', { session: false}), function(req, res) {
    var token = getToken(req.headers);
    if (token) {
      Book.find(function (err, books) {
        if (err) return next(err);
        res.json(books);
      });
    } else {
      return res.status(403).send({success: false, msg: 'Unauthorized Access...'});
    }
  });

  getToken = function (headers) {
    if (headers && headers.authorization) {
      var parted = headers.authorization.split(' ');
      if (parted.length === 2) {
        return parted[1];
      } else {
        return null;
      }
    } else {
      return null;
    }
  };

module.exports = router;

答案 1 :(得分:1)

要梳理的代码很多。你能举一个较小的例子吗?

根据您的堆栈跟踪,问题出在

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中,因此您可以直接使用它。