Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have some indentation issues with your code but that could just be because of copy paste. Anyways adding a line <code>sum = 0</code> at the beginning of your <code>TrapArea</code> function should solve your current error. But as @Blender pointed out in the comments, you have another issue, which is the lack of a multiplication operator (<code>*</code>) after your floating point division expression <code>(1.0/2.0)</code>. </p> <p>Remember that in Python expressions are not always evaluated as you would expect mathematically. Thus <code>(a op b)(c)</code> will not automatically multiply the result of <code>a op b</code> by <code>c</code> like you would expect with a mathematical expression. Instead this is the function call notation in Python. </p> <p>Also remember that you must initialize all variables before using their values for assignment. Python has no default value for unnamed variables so when you reference the value of <code>sum</code> with <code>sum += expr</code> which is equivalent to <code>sum = sum + expr</code> you are trying to reference a name (<code>sum</code>) that is not binded to any object at all.</p> <p>The following revision to your function should do the trick. Notice how I place multiplication operators (<code>*</code>) between every expression that you intend to multiply.</p> <pre><code>def TrapArea(n): sum = 0 for i in range(1, n): i = float(i) deltax = (pi-0)/n sum += (1.0/2.0)*(((pi-0)/n)*(sin((i-1)/n*(pi-0))) + sin((i/n)*(pi-0)))*deltax return sum </code></pre> <p>EDIT: I also dealt with the float division issue by converting <code>i</code> to <code>float(i)</code> within every iteration of the loop. In Python 2.x, if you divide one integer type object with another integer type object, the expression evaluates to an integer regardless of the actual value.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload