2017-03-03 148 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