Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python NameError中未定义数组名称_Python - Fatal编程技术网

python NameError中未定义数组名称

python NameError中未定义数组名称,python,Python,我是python的初学者,开始学习基础知识。我这里有一个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 leng

我是python的初学者,开始学习基础知识。我这里有一个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)
在for循环中,当我尝试assing t[I]时,它抛出一个错误“NameError:name't'未定义”。在一些stackoverflow问题中,有一些建议可以使用

from array import array
但我还是犯了一个错误。我试着从另一个角度来解决这个问题。请帮助排除此错误

谢谢


Jdbaba

您必须先创建
t
,然后才能对其进行索引,请尝试以下操作:

m = xf/dx
t = []
for i in range(0,int(n)) :
    t.append(i*tf/n)
您还可以从数组导入数组中取出
行。您正在创建和使用列表,而不是数组。如果您只是在学习python,那么可能不需要数组和列表

实际上,您甚至可以用以下内容替换整个for循环:

m = xf/dx
t = [i*tf/n for i in range(int(n))]

这个版本使用列表理解来完成同样的事情。在我看来,它几乎肯定更快更容易理解。

非常感谢您的回复。当我实施你的建议时,它说“IndexError:list assignment index out range”。@Jdbaba是的,我忘记了这一部分,它现在应该被修复了。@Matt非常感谢。这两种方法现在都能很好地使用。@Matt我怎样才能在python中编写这样的二维数组
For i=0 To na(i,0)=0下一个i
实际上我来自Vb背景,试图用Python转换现有代码。谢谢。@Jdbaba我不太确定你在找什么。您可能应该创建一个新问题。