【问题标题】:Syntax Error with SimPy yield hold [closed]SimPy yield hold 的语法错误[关闭]
【发布时间】:2015-03-18 11:43:39
【问题描述】:

我是 Simpy 的初学者。我只是按照手册 (http://simpy.sourceforge.net/old/SimPy_Manual/Manuals/Manual.html) 的第一步进行操作,并将其调整为我想做的事情。 我的代码

# -*- coding: utf-8 -*-
#from mpl_toolkits.mplot3d import Axes3D
#from matplotlib import cm
#import matplotlib.pyplot as plt
import numpy as np
import random as rd
import math
from SimPy.Simulation import Process, activate, hold, initialize, simulate
import sys

class Agent(Process):
    def __init__(self,i,x,y,u,v,state):
        Process.__init__(self, name='Agent' + str(i))
        self.i = i
        self.x = x
        self.y = y
        self.u = u
        self.v = v
        self.st = state

    def go(self):
        dt=1.0/math.sqrt(self.u**2+self.v**2)
        print('%s starts at %s' %(self.i,now())
        yield hold, self, dt
        print('%s changed place at %s' %(self.i,now())

initialize()
a = Agent('nano',9,0,0,1,0)
#b = Agent(1,9,0,0,1,0)
activate(a,a.go(),at=0.0)
#activate(b,b.go(),at=5.0)
simulate(until=100.0)

引发以下错误:

hcecilia@helcecil:~/BRL$ python agent_sim.py 
  File "agent_sim.py", line 24
    yield hold, self, dt
        ^
SyntaxError: invalid syntax

但是当我尝试手册的确切代码时,它是:

from SimPy.Simulation import Process, activate, initialize, hold, now, simulate


class Message(Process):
    """A simple Process"""
    def __init__(self, i, len):
        Process.__init__(self, name='Message' + str(i))
        self.i = i
        self.len = len

    def go(self):
        print('%s %s %s' % (now(), self.i, 'Starting'))
        yield hold, self, 100.0
        print('%s %s %s' % (now(), self.i, 'Arrived'))


initialize()
p1 = Message(1, 203)   # new message
activate(p1, p1.go())  # activate it
p2 = Message(2, 33)
activate(p2, p2.go(), at=6.0)
simulate(until=200)
print('Current time is %s' % now())  # will print 106.0

它工作正常。我看不出两者有什么区别。如果你有什么想法...

【问题讨论】:

标签: python syntax generator yield simpy


【解决方案1】:

您错过了前一行的结束 ) 括号:

print('%s starts at %s' %(self.i,now())
#    1                   2          332?

括号对号1没有闭合。

在 Python 中,逻辑行仅在所有圆括号、方括号和大括号都闭合时才结束;没有右括号 yield 被视为 print() 函数调用的一部分,这是无效的语法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-05
    • 2018-09-23
    • 2014-02-12
    • 2012-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多