Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
373 views
in Technique[技术] by (71.8m points)

module - nameError: name 'pivots' is not defined

Could you help me to solve the following problem,please ? I did write a module in pyhton . For the next module I need the following variables or values: lastprice and lastpivotprice

What definition or script change is neccessary, that this 2 variables are global variables)?

Actually if i try to call lastpivotprice outside the module i get the following error message: nameError: name 'pivots' is not defined

Module Code: checkpivots.py

    try:  
      df['High'].plot(label='high')

      pivots =[]
      dates = []
      counter = 0
      lastPivot = 0

      Range = [0,0,0,0,0,0,0,0,0,0]
      daterange = [0,0,0,0,0,0,0,0,0,0]

      for i in df.index:
        currentMax = max(Range , default=0)
        value=round(df["High"][i],2)
        Range=Range[1:9]
        Range.append(value)
        daterange=daterange[1:9]
        daterange.append(i)

      if currentMax == max(Range , default=0):
          counter+=1
      else:
          counter = 0
      if counter ==  5:
          lastPivot=currentMax
          dateloc =Range.index(lastPivot)
          lastDate = daterange[dateloc]

          pivots.append(lastPivot)
          dates.append(lastDate)
   
            
    except Exception:
      print("-")

    lastpivotprice = pivots[-1]
    
    lastprice=df.iloc[-1]['Close'] #read value in the last row in col 'close'
    lastprice2 = df['Close'].values[-2] #read value in the last row minus2 in col 'close'
   
    #print (lastprice)        
    # print (lastpivotprice)
    # print (lastprice2)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you want to take your variables in every module as globals, you have to set them as global variables. For example see this code below:

c = 0 # global variable

def add():
    global c
    c = c + 2 # increment by 2
    print("Inside add():", c)

add()
print("In main:", c)

If you delete global from c, the c remains 0 in MAIN.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...