我试图从微仿真软件PTV VISSIM运行此脚本。并得到此错误。分配源之前引用了局部变量'Veh_C2X_attributes'?在我的脚本中,我使用列表推导创建了一个名为“ Veh_C2X_attributes”的列表。任何帮助对此将不胜感激。谢谢
脚本。
def Init():
#Global Parameters
global distDistr
global Vehicle_Type_C2X_no_message
global Vehicle_Type_C2X_HasCurrentMessage
Veh_attributes = None
distDistr = 1 # number of Distance distribution used for sending out a C2X message
Vehicle_Type_C2X_no_message = '101' # number of C2X vehicle type (no active message) has to be a string!
Vehicle_Type_C2X_HasCurrentMessage = '102' # number of C2X vehicle type with active message has to be a string!
return
def Main():
# Get several attributes of all vehicles:
Veh_attributes = Vissim.Net.Vehicles.GetMultipleAttributes(('Pos', 'VehType', 'No'))
if len(Veh_attributes) > 0: # Check if there are any vehicles in the network:
# Filter by VehType C2X:
Veh_C2X_attributes = None
Veh_C2X_attributes = [item for item in Veh_attributes if item[1] == Vehicle_Type_C2X_no_message or item[1] == Vehicle_Type_C2X_HasCurrentMessage] #Getting Error here
#Check if the Vehicle is in the range
for C2X_Veh in range(len(Veh_C2X_attributes)):
if Veh_C2X_attributes[C2X_Veh][0]==300:
Veh_sending_Msg = Vissim.Net.Vehicles.ItemByKey(Veh_C2X_attributes[C2X_Veh][3])
Coord_Veh = Veh_sending_Msg.AttValue('CoordFront') # reading the world coordinates (x y z) of the vehicle
PositionXYZ = Coord_Veh.split(" ")
Pos_Veh_SM = Veh_sending_Msg.AttValue('Pos') # relative position on the current link
Veh_sending_Msg.SetAttValue('C2X_HasCurrentMessage', 1)
Veh_sending_Msg.SetAttValue('C2X_SendingMessage', 1)
Veh_sending_Msg.SetAttValue('C2X_MessageOrigin', Pos_Veh_SM)
# Getting vehicles which receive the message:
Veh_Rec_Message = Vissim.Net.Vehicles.GetByLocation(PositionXYZ[0], PositionXYZ[1], distDistr)
return
答案 0 :(得分:-1)
您的if len(Veh_attributes) > 0:
失败了,因此未定义Veh_C2X_attributes
。
一种确定方法是通过在print
中放入if len(Veh_attributes) > 0:
来检查该条件是否正在执行。
更好的做法是在之前定义Veh_C2X_attributes
。
例如:
def Main():
# Get several attributes of all vehicles:
Veh_C2X_attributes = [] # this will prevent code from failing
Veh_attributes = Vissim.Net.Vehicles.GetMultipleAttributes(('Pos', 'VehType', 'No'))
if len(Veh_attributes) > 0: # Check if there are any vehicles in the network:
# Filter by VehType C2X:
Veh_C2X_attributes = [item for item in Veh_attributes if item[1] == Vehicle_Type_C2X_no_message or item[1] == Vehicle_Type_C2X_HasCurrentMessage] #Getting Error here
#Check if the Vehicle is in the range
for C2X_Veh in range(len(Veh_C2X_attributes)):
if Veh_C2X_attributes[C2X_Veh][0]==300:
Veh_sending_Msg = Vissim.Net.Vehicles.ItemByKey(Veh_C2X_attributes[C2X_Veh][3])
Coord_Veh = Veh_sending_Msg.AttValue('CoordFront') # reading the world coordinates (x y z) of the vehicle
PositionXYZ = Coord_Veh.split(" ")
Pos_Veh_SM = Veh_sending_Msg.AttValue('Pos') # relative position on the current link
Veh_sending_Msg.SetAttValue('C2X_HasCurrentMessage', 1)
Veh_sending_Msg.SetAttValue('C2X_SendingMessage', 1)
Veh_sending_Msg.SetAttValue('C2X_MessageOrigin', Pos_Veh_SM)
# Getting vehicles which receive the message:
Veh_Rec_Message = Vissim.Net.Vehicles.GetByLocation(PositionXYZ[0], PositionXYZ[1], distDistr)
return