I am a beginner in python and started to learn basics. I have a sample program here in python
#Simple Program Python
tf=float(input("Enter total time of run in seconds "))
delt = float(input("Enter the time step of run "))
dx = float(input("Enter the value of dx "))
xf = float(input("Enter the total length of a channel "))
n =float(tf/delt)
#t =range[n]
from array import array
m = xf/dx
for i in range(0,int(n)) :
t[i]=i*tf/n
print("THe total time of run is %r " %tf)
print("seconds")
print("The time step is %r" %delt)
print("Number of steps is %r" %n)
print("The number of space step in X direction is %r " %m)
In the for loop, when I try to assing t[i], then it throws an error “NameError: name ‘t’ is not defined”. In some stackoverflow questions, there was suggestions to use
from array import array
But I still get an error. I tried that solution from NameError: name 'array' is not defined in python. PLease help to get rid of this error.
Thanks.
Jdbaba
You have to create
tbefore you can index into it, try this:You also can take out the
from array import arrayline. You are making and using a list not an array. If you are just learning python you probably don’t need arrays and lists will do just fine.Actually, you can even replace the entire for loop with this:
This version uses a list comprehension to accomplish the same thing. It is almost certainly faster and (in my opinion) easier to understand.