使我的python脚本正常工作并调用我的变量时遇到问题

时间:2019-06-05 12:55:13

标签: python arrays python-3.x

我正在为管道类构建一个python脚本,并在主脚本中定义一些变量,并制作了一个可执行脚本,该可执行脚本调用主脚本并为具有真或假逻辑的区域赋值,以确定是否需要是否适合某个地区。我对python不太了解,并且确实会对此有所帮助。我将包括与提到的问题有关的脚本部分。

#this is the main program 

import numpy as np
import sys

uCounter = 0

class Pipe(object):
   global uCounter

   def __init__(self, nPorts, volume):
      self.volume = volume
      self.rhoj = D(1)
      self.Pj = D(1)
      self.nPorts = nPorts  
      self.freePorts = []
      self.portSetm = []  
      self.portSetP = []
      self.portSetValue = []  
      self.portSetAreas = []  
      self.portSetAreasValue = [] 
      for i in range(nPorts):
         self.freePorts.append(i)
         self.portSetm.append(False)
         self.portSetP.append(False)
         self.portSetValue.append(0.0)
         self.portSetAreas.append(False)
         self.portSetAreasValue.append(0.0)
      self.m = D(self.nPorts)
      self.P = D(self.nPorts)
      self.Areas = D(self.nPorts)
      self.pd = np.zeros((self.nPorts))
      self.dt = 0.0  

   def logics(self, freePorts):                                                                                  
      for port in range(nPorts):
         if self.portSetAreas[self.freePorts[port]] != 0:
            self.Areas[port] = self.portSetAreasValue[self.freePorts[port]]

   def Values1(self):
      equation = Equation(1, uCounter)
      for p in range(self.nPorts):
         equation.A[0, self.m.gid[p]] = self.Areas[port]
      equation.c[0] = 0.0
      return(equation)

#and this is the executable 

import math
import numpy as np
import sys
import scipy.io
import mainproject

Pipe1.portSetAreas[0] = True
Pipe1.portSetAreasValue[0] = 0.1
Pipe1.portSetAreas[1] = True
Pipe1.portSetAreasValue[1] = 0.1

#0 refers to the inlet of the pipe
#1 refers to the outlet of the pipe
equation.A[0,self.m.gid[p]] = self.Areas[port]
NameError: global name 'port' is not defined

1 个答案:

答案 0 :(得分:0)

您是在for循环中定义p而不是port

     for p in range(self.nPorts):
         equation.A[0, self.m.gid[p]] = self.Areas[port]

也就是说,您的代码中还存在其他逻辑错误。

此代码也将导致错误:

   def logics(self, freePorts):                                                                                  
      for port in range(nPorts):

您应该将范围通话更改为range(self.nPorts)