Please make sure to use this year's server!
https://compphys-2324.notebooks.danielmaitre.phyip3.dur.ac.uk
Error types in Python guide you to the issue.
The last lines of the traceback are key.
Inspect the line where the error occurs and its predecessor.
Infinite loops pose a unique debugging challenge because they keep running, causing either:
Implement a loop counter as a best practice to prevent infinite loops.
icounter = 0
while True and icounter < 10000:
icounter += 1
# Execute some code
if condition: # Replace 'condition' with your actual conditional statement
break
if icounter == 10000:
print("Loop terminated due to counter.")
An Off-by-One error occurs when a loop iterates one time too few or too many.
In the following example, the loop runs nsteps
times:
for i in range(nsteps):
result[i] = ...
The loop's highest index value is i = nsteps - 1
.
The function range(beg, end)
iterates from beg
to end - 1
, covering end - beg
steps.
Focusing on the first element and total steps often simplifies reasoning about loop boundaries.
This error arises when using array syntax x[i]
on a non-subscriptable object like x
.
def f():
return [1, 2, 3, 4]
f[1]
In this case, f
is a function. To make it subscriptable, invoke it first:
f()[1]
With xxx either float
or int
. This happens normally when parenthesis are forgotten.
3(x-y)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-23-112ceb8d6610> in <module>() ----> 1 3(x-y) TypeError: 'int' object is not callable