2017-05-31 57 views
-6

我是Python的初學者。我有以下代碼不會返回任何內容。托馬斯算法python

有沒有人有答案爲什麼?當我執行代碼時,幾乎沒有任何反應。

尋找矩陣的大小和確定N個

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

歡迎來到StackOverflow。請閱讀並遵守幫助文檔中的發佈準則。 [最小,完整,可驗證的示例](http://stackoverflow.com/help/mcve)適用於此處。在發佈您的MCVE代碼並準確描述問題之前,我們無法爲您提供有效的幫助。 我們應該能夠將發佈的代碼粘貼到文本文件中,並重現您描述的問題。也許你需要的只是一個可以調用這個功能的單線主程序,但我們需要確定。 – Prune

+0

你的'exit'調用在'if'之外,並且始終執行 – Copperfield

+0

Thx作爲響應,但即使沒有退出也不會發生任何事情...... –

回答

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)是outdented標籤太遠。縮進這一行。請記住,與大多數其他語言不同,Python的執行結構基於間距。對於if語句之類的內容,在該語句下縮進的所有內容都與if (something) { }相同。

if (something): 
     do this 
     and this 
but this will happen regardless