我是Python的初学者。我有以下代码,不会返回任何内容。
有没有人回答为什么?当我执行代码时,几乎没有任何反应。
def thomas(a,b,c,d):
a= [1,3,1.5,4.5,4.5]
b= [-6,-4.5,-7.5,-7.5,-4.5]
c= [3,3,3,3,3]
d= [0,0,100,0,0]
n = len(b)
#print n # Used for debugging
# Test the size of a and c
if len(a) != n-1:
print ('Wrong index size for a.\n A should have an index of'), n-1, '\n
Your a has ', len(a)
exit()
if len(c) != n-1:
print ('Wrong index size for c.\n C should have an index of'), n-1, '\n
Your c has', len(c)
exit()
# Converting to float and appending 0.0 to c
for i in range(0,len(a)):
a[i] = float(a[i])
for i in range(0,len(b)):
b[i] = float(b[i])
for i in range(0,len(c)):
c[i] = float(c[i])
for i in range(0,len(d)):
d[i] = float(d[i])
c.append(0.0) # Hack to make the function to work
# Calculate p and q
p = []; q= []
p.append(c[0]/b[0]); q.append(d[0]/b[0])
for j in range(1,n):
pj = c[j]/(b[j] - a[j-1]* p[j-1])
qj = (d[j] - a[j-1]*q[j-1])/(b[j] - a[j-1]* p[j-1])
p.append(pj); q.append(qj)
#print p,q # Used for debugging the code!
# Back sub
x = []; x.append(q[n-1])
for j in range(n-2,-1,-1):
xj = q[j] - p[j]*x[0] # Value holder
x.insert(0,xj) # Building the list backwards
# Return the value
return x
答案 0 :(得分:1)
你的缩进很棘手。
在第13行和第17行,缩进exit
语句,使其仅在满足条件(if语句)时调用:
if len(a) != n-1:
print ('Wrong index size for a.\n A should have an index of')
exit()
if len(c) != n-1:
print ('Wrong index size for c.\n C should have an index of')
exit()
同样,c.append(0.0)
缩进了一个标签太远了。也缩进这一行。
请记住,与大多数其他语言不同,Python将其执行结构基于间距。对于像if语句这样的东西,在该语句下缩进的所有内容都被认为与if (something) { }
相同。
if (something):
do this
and this
but this will happen regardless