Debugging and Common Exam Traps - Exam 1

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play

1/29

encourage image

There are no tags or description

Looks like no tags are added yet.

30 Terms

1
New cards

Why does print(y) fail if y has not been assigned?

NameError: y is not defined yet.

2
New cards

Why is if x=3: invalid Python?

Assignment was used in a condition; write if x==3:

3
New cards

Why does x = np.array([1,2,3]); x[3] fail?

Out-of-range index: valid positions are 0,1,2

4
New cards

Why does np.sqrt(4+5) differ from np.sqrt(4)+5?

Parentheses change which expression is under the square root.

5
New cards

What must follow if, elif, else, and for headers?

A colon : and an indented body.

6
New cards

How should you trace code by hand on an exam?

Track variable values line by line; use a small table for loops.

7
New cards

Debug: radius = np.sqrt(1+8*np.exp(1) // missing final parenthesis

Correct to radius = np.sqrt(1 + 8*np.exp(1))

8
New cards

Debug: area = np.pi*radius^2

Use radius**2: area = np.pi*radius**2

9
New cards

Debug: x = np.array(1,2,3)

Use brackets for data: x = np.array([1,2,3])

10
New cards

Debug: print(x[3]) for x=np.array([1,2,3]

IndexError; use indices 0,1,2 or x[-1] for the final value.

11
New cards

Debug: y=x+2 when x is undefined

Define x first, then calculate y.

12
New cards

Debug: a=np.linspace(0,1,0.1) for spacing 0.1

Use 11 points: np.linspace(0,1,11)

13
New cards

Debug: x=np.arange(0,5,5) when five unit-spaced values wanted

Use np.arange(0,5,1), giving [0,1,2,3,4]

14
New cards

Debug: y=np.abs(x[x>0]) when all nonzero magnitudes wanted

Use np.abs(x[x!=0]); x>0 excludes negatives.

15
New cards

Debug: x[x>0 and x<5] for a NumPy array

Use x[(x>0) & (x<5)] with elementwise &

16
New cards

Debug: loop sum starts at total=1 for summing 1..4

Use total=0 to avoid adding an extra 1

17
New cards

Debug: for k in range(1,n): when sum should include n

Use range(1,n+1) because the stop is excluded.

18
New cards

Debug: for k in range(5): x[k+1]=... on x=np.zeros(5)

Last index k+1=5 is out of bounds; use range(4)

19
New cards

Debug: for k in range(4) print(k)

Add a colon: for k in range(4):

20
New cards

Debug: if x>0: followed by an unindented print inside its block

Indent the body, for example four spaces before print(...)

21
New cards

Debug: if np.sin(np.pi)==0 expects exact equality

Floating-point values can be slightly off; test abs(np.sin(np.pi))<tol

22
New cards

Debug: if x>0: when x is a whole NumPy array

Use (x>0).any()/.all() for one Boolean, or x[x>0] for selection

23
New cards

Debug: y=x; x[0]=9 changed y too

Use y=x.copy() if you need independent arrays

24
New cards

Debug: trying to call np.pi()

Remove parentheses: np.pi is a constant

25
New cards

Debug: root-finding chooses [a,m] even though f(a),f(m) have same sign

Keep the half with opposite endpoint signs instead

26
New cards

Debug: checking f(a)==0 only and assuming there is no interior root

Evaluate a bracket and midpoint; roots can be between endpoints

27
New cards

Debug: using (a+b) / 2 but never updating a or b

Update one endpoint after testing signs so the bracket shrinks.

28
New cards

Debug: plotting y=x**2 with no x array defined

Create an x grid before calculating y, e.g. x=np.linspace(-2,2,41)

29
New cards

Debug: code prints y before assigning y on a later line

Move the print below the assignment to y

30
New cards

Debug: forgetting to use one variable from an earlier step

Check names and execution order; use the assigned variable consistently