I have a main python code with variables that I wish to control from a separate python script. I get a name error message and I am unsure how to resolve this. Has anyone encountered a similar challenge? How can I control the variable n from a separate python script?
from main import *
npart = 5
NameError Traceback (most recent call last)
Input In [12], in <module>
----> 1 from main import *
2 npart = 5
File ~/main.py:91, in <module>
88 latmin = 45.977778
89 latmax = 45.931389
---> 91 npart = n # number of particles to be released
92 for i in range(n):
93 lon = lonmin + random.uniform(0, 1)*(lonmax-lonmin)
NameError: name 'n' is not defined
The main code that I am trying to control looks like this. main.py. Here I type in 5 so the code will run without error. I would like to call it a variable n here. In the separate code define n = 5 and reproduce the results.
import random
x = []
y = []
lonmin = -63.226111
lonmax = -63.229722
latmin = 45.977778
latmax = 45.931389
npart = 5 # number of particles to be released
for i in range(5):
lon = lonmin + random.uniform(0, 1)*(lonmax-lonmin)
x.append(lon)
lat = latmin + random.uniform(0, 1)*(latmax-latmin)
y.append(lat)
print(x, y)
n?