-2

我正在尝试创建一个当前仅包含以下内容的动态函数:

"chosen = random.choice([<ClassLibrary.Field object at 0x0330FA48> , <ClassLibrary.Field object at 0x0330FAA8> , <ClassLibrary.Field object at 0x0330FB08> , <ClassLibrary.Field object at 0x0330FB98> , <ClassLibrary.Field object at 0x0330FC28> , <ClassLibrary.Field object at 0x0330FCB8> , <ClassLibrary.Field object at 0x0330FD48> , <ClassLibrary.Field object at 0x0330FDD8> , <ClassLibrary.Field object at 0x0330FE68>])"

这意味着我有一个变量设置为等于列表中的随机元素。但是我得到了一个语法错误,大概是因为列表对象是对这些对象在内存中的空间的引用?choice() 与原始列表一起使用,如下所示:

chosen = random.choice(ticTacToe.fields)

我得到的错误是:

  File "C:\Users\churc\Documents\P7\P7 Modular Playtesting\ticTacToe2.py", line 267, in <module>
    eval(FunctionLibrary.makeExecutableStatement(ticTacToe.players[1].actions[0].statements[0]))
  File "<string>", line 1
    chosen = random.choice([<ClassLibrary.Field object at 0x0330FA48> , <ClassLibrary.Field object at 0x0330FAA8> , <ClassLibrary.Field object at 0x0330FB08> , <ClassLibrary.Field object at 0x0330FB98> , <ClassLibrary.Field object at 0x0330FC28> , <ClassLibrary.Field object at 0x0330FCB8> , <ClassLibrary.Field object at 0x0330FD48> , <ClassLibrary.Field object at 0x0330FDD8> , <ClassLibrary.Field object at 0x0330FE68>]) 
           ^
SyntaxError: invalid syntax
[Finished in 0.1s with exit code 1]

有人知道我在做什么错吗?以及如何纠正?

4

1 回答 1

0

<ClassLibrary.Field object at 0x0330FA48>不是有效的 python 语法,因为它只是对象实例的输出字符串。没有足够的信息来提供关于您的代码应该做什么的完整答案,但从您所写的内容来看,我建议在您之前声明random.choice()的实例列表中使用。ClassLibrary.Field

例子:

instances_list = [ ClassLibrary.Field() for instance in range(4) ]
chosen = random.choice(instances_list)
于 2019-11-20T18:53:23.997 回答