2017-03-03 142 views
0

這是我的代碼,語法錯誤是在第18行。我努力理解爲什麼我得到這個錯誤。語法錯誤無效,但我無法發現它? (Python)

from visual import * 

def Efield(pos, charge) 

    """ 
    Calculate electric field due to point charge 

    Inputs 
    ====== 

    pos  - Position where the E field is required (vector) 
    charge.pos - Position of the charge (vector) 
    charge.q - Charge (float) 

    Returns 
    ======= 

    The electric field vector 
    """ 

    r = vector(charge.pos) - vector(pos) # Vector from pos to chargepos 

    E = float(q) * r/abs(r)**3 # Coulomb's law 

    return E 

創建一些費用

charge1 = sphere(pos=(0,2,0), q=1, radius=0.2, color=color.red) # Positive charge 
charge2 = sphere(pos=(0,-2,0), q=-1, radius=0.2, color=color.blue) # Negative charge 

在隨機位置

random.seed(1234) # Specify a random seed 

n = 500 
for i in range(n): 
    x = random.uniform(-4 4) 
    y = random.uniform(-4,4) 
    z = random.uniform(-4,4) 

    pos = vector(x,y,z) 

    # Make sure pos is not too close to both positive and negative charge 
    if abs(pos - charge1.pos) > 1 and abs(pos - charge2.pos) > 1: 
    # Create an arrow if it's far enough from both charges 
    a = arrow(pos=pos, axis= Efield(pos, charge1) + Efield(pos,charge1)) 
+0

解決您的壓痕 – depperm

+0

廣告四個空格來縮進添加到您的代碼'」「 ' –

回答

2

你缺少生成箭頭的:def Effield(pos, charge)

您還缺少上線一個逗號:

x = random.uniform(-4 4) 

應該

x = random.uniform(-4, 4) 

而且這個功能是沒有縮進可言,這將引發:

IndentationError: expected an indented block

+1

和縮進。 – Alfe

+0

好的,謝謝你,我解決了這個問題。現在我收到第19行的另一個語法錯誤。數字4突出顯示。 – timetabedlidalot

+0

@timetabedlidalot你沒有行號,我們可以看到... – River