Python 局部变量';Veh#u C2X#u属性';在分配源之前引用?
我试图从一个微型模拟软件PTV VISSIM运行这个脚本。并且得到了这个错误。分配源之前引用的局部变量“Veh_C2X_attributes”??在我的脚本中,我使用列表理解创建了一个名为“Veh_C2X_attributes”的列表。任何有关这方面的帮助都将不胜感激。谢谢 剧本Python 局部变量';Veh#u C2X#u属性';在分配源之前引用?,python,variables,Python,Variables,我试图从一个微型模拟软件PTV VISSIM运行这个脚本。并且得到了这个错误。分配源之前引用的局部变量“Veh_C2X_attributes”??在我的脚本中,我使用列表理解创建了一个名为“Veh_C2X_attributes”的列表。任何有关这方面的帮助都将不胜感激。谢谢 剧本 def Init(): #Global Parameters global distDistr global Vehicle_Type_C2X_no_message global Veh
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
如果len(Veh_属性)>0:失败,则的车辆C2X_属性
未定义
确保这一点的一种方法是在if len(Veh_属性)>0:
中放入print
,以检查是否正在执行该条件
另外,更好的做法是在之前定义Veh_C2X_属性
例如:
如果有人能说服我为什么投票被否决,我会删除/编辑这个答案。
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